nschloe / tikzplotlib

:bar_chart: Save matplotlib figures as TikZ/PGFplots for smooth integration into LaTeX.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Scatter plots with `scatter` argument ignore `edgecolors`

ORippler opened this issue · comments

Description

Scatter plots that use a colormap (and therefore need to contain the scatter bool, refer #479 ) currently do not handle marker modifications such as edgecolors correctly.

The reason behind this is that as soon as scatter is present, the marker appearances need to be accessed via

/scatter/use mapped color (refer pgfplots documentation), and the edgecolors passed to the draw keyword there (they are ignored in the \addplot comnand if scatter is present).

MWE

import numpy as np
import matplotlib.pyplot as plt
import tikzplotlib
from sklearn.datasets import make_blobs

x, y = make_blobs(centers=[[4, -4], [-4, -4], [-1.5, 3], [0, -2]], cluster_std=[0.8, 0.7, 1.0, 1.2],
               random_state=0, n_samples=400, n_features=2)

plt.scatter(x[:,0], x[:,1], c=y, edgecolors='k', cmap='viridis')

plt.savefig("test.png", dpi=600)
tikzplotlib.save("test.tex", standalone=True)

gives the following image in matplotlib:

test

and via tikzplotlib:

image

Workaround

If the edgecolor is known beforehand, it can be passed to axis via extra_axis_parameters:

import numpy as np
import matplotlib.pyplot as plt
import tikzplotlib
from sklearn.datasets import make_blobs

x, y = make_blobs(centers=[[4, -4], [-4, -4], [-1.5, 3], [0, -2]], cluster_std=[0.8, 0.7, 1.0, 1.2],
               random_state=0, n_samples=400, n_features=2)

plt.scatter(x[:,0], x[:,1], c=y, edgecolors='k', cmap='viridis')

plt.savefig("test.png", dpi=600)
tikzplotlib.save("test.tex", standalone=True, extra_axis_parameters={
        "scatter/use mapped color={draw=black,fill=mapped color}",
    },)

yields the correct image:

image

commented

I confirm this. Even worse, plots with mapped colors forget about the marker shape itself:

import matplotlib.pyplot as plt
import tikzplotlib

plt.scatter([1.0, 2.0], [3.0, 4.0], marker="x")
plt.scatter([1.0, 2.0], [3.0, 4.0], marker="x", cmap="viridis")
plt.scatter([1.0, 2.0], [3.0, 4.0], c=[0.0, 1.0], marker="x")
plt.scatter([1.0, 2.0], [3.0, 4.0], c=[0.0, 1.0], marker="x", cmap="viridis")

tikzplotlib.save("test.tex")
% This file was created with tikzplotlib v0.10.1.
\begin{tikzpicture}

\definecolor{darkgray176}{RGB}{176,176,176}
\definecolor{darkorange25512714}{RGB}{255,127,14}
\definecolor{steelblue31119180}{RGB}{31,119,180}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xmin=0.95, xmax=2.05,
xtick style={color=black},
y grid style={darkgray176},
ymin=2.95, ymax=4.05,
ytick style={color=black}
]
\addplot [draw=steelblue31119180, fill=steelblue31119180, mark=x, only marks]
table{%
x  y
1 3
2 4
};
\addplot [draw=darkorange25512714, fill=darkorange25512714, mark=x, only marks]
table{%
x  y
1 3
2 4
};
\addplot [colormap/viridis, only marks, scatter, scatter src=explicit]
table [x=x, y=y, meta=colordata]{%
x  y  colordata
1 3 0.0
2 4 1.0
};
\addplot [colormap/viridis, only marks, scatter, scatter src=explicit]
table [x=x, y=y, meta=colordata]{%
x  y  colordata
1 3 0.0
2 4 1.0
};
\end{axis}

\end{tikzpicture}

As you can see, the two last plots miss the mark attribute.