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

`ax.scatter` with `cmap` ignores `alpha`

ORippler opened this issue · comments

Description

When performing scatter combined with a cmap and alpha, the alpha value is ignored by tikzplotlib.

MWE

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

# Define a dataset with 2 labels and thus colors
data, label = make_blobs(centers=[[0, 0], [2,2], [-2,2]], cluster_std=[0.8, 0.8, 0.8],
               random_state=0, n_samples=20, n_features=2)

colors = [(1, 0, 0), (0,1,0), (0,0,1)]
test_cmap = LinearSegmentedColormap.from_list("test", colors, N=512)

fig, ax = plt.subplots(1, 1, figsize=(6,6))

# ax.scatter(data[:, 0], data[:, 1], c=_colors[label]) # this fails
ax.scatter(data[:, 0], data[:, 1], c=label, cmap=test_cmap, alpha=0.2) # this works

plt.savefig("test.png")

tikzplotlib.save(
    "test.tex",
    standalone=True
)

Expected figure:

test

Result:
image

Broken TeX:

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\DeclareUnicodeCharacter{2212}{−}
\usepgfplotslibrary{groupplots,dateplot}
\usetikzlibrary{patterns,shapes.arrows}
\pgfplotsset{compat=newest}
\begin{document}
% This file was created by tikzplotlib v0.9.8.
\begin{tikzpicture}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-3.04362698716235, xmax=4.14920208824834,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=-0.986830627035054, ymax=3.38049533905415,
ytick style={color=black}
]
\addplot [
  colormap={mymap}{[1pt]
  rgb(0pt)=(1,0,0);
  rgb(1pt)=(0,1,0);
  rgb(2pt)=(0,0,1)
},
  only marks,
  scatter,
  scatter src=explicit
]
table [x=x, y=y, meta=colordata]{%
x  y  colordata
-2.27832971946092 2.12507917528318 2.0
0.115234856928702 1.16341880557038 0.0
0.782990387284591 1.79271455936117 0.0
1.49404639211997 -0.781822303901129 0.0
2.25045416132072 1.31672340855862 1.0
2.35509058619634 2.26693946189941 1.0
1.41124187677413 0.320125766693779 0.0
3.19526325852608 1.83587338898736 1.0
-2.71022859810409 0.415362825420858 2.0
-1.01576745541782 2.96190387902753 2.0
-2.30986145392636 1.75815779953973 2.0
2.03660681384116 1.85025291997933 1.0
0.608830180117595 0.0973400131942627 0.0
-1.87604205944247 2.30253001568174 2.0
2.6915489590876 1.40626798367485 1.0
-0.0825750814348463 0.328478801550698 0.0
-0.0423918526672629 2.52289487635229 1.0
3.81580369919009 0.836507460320988 1.0
0.760070734020472 -0.121085766638158 0.0
-0.773776628513234 3.17548701592023 2.0
};
\end{axis}

\end{tikzpicture}

\end{document}

Fixed TeX

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\DeclareUnicodeCharacter{2212}{−}
\usepgfplotslibrary{groupplots,dateplot}
\usetikzlibrary{patterns,shapes.arrows}
\pgfplotsset{compat=newest}
\begin{document}
% This file was created by tikzplotlib v0.9.8.
\begin{tikzpicture}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-3.04362698716235, xmax=4.14920208824834,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=-0.986830627035054, ymax=3.38049533905415,
ytick style={color=black}
]
\addplot [
  colormap={mymap}{[1pt]
  rgb(0pt)=(1,0,0);
  rgb(1pt)=(0,1,0);
  rgb(2pt)=(0,0,1)
},
  only marks,
  opacity=0.2, %                                                 **THIS HERE WAS ADDED**
  scatter,
  scatter src=explicit
]
table [x=x, y=y, meta=colordata]{%
x  y  colordata
-2.27832971946092 2.12507917528318 2.0
0.115234856928702 1.16341880557038 0.0
0.782990387284591 1.79271455936117 0.0
1.49404639211997 -0.781822303901129 0.0
2.25045416132072 1.31672340855862 1.0
2.35509058619634 2.26693946189941 1.0
1.41124187677413 0.320125766693779 0.0
3.19526325852608 1.83587338898736 1.0
-2.71022859810409 0.415362825420858 2.0
-1.01576745541782 2.96190387902753 2.0
-2.30986145392636 1.75815779953973 2.0
2.03660681384116 1.85025291997933 1.0
0.608830180117595 0.0973400131942627 0.0
-1.87604205944247 2.30253001568174 2.0
2.6915489590876 1.40626798367485 1.0
-0.0825750814348463 0.328478801550698 0.0
-0.0423918526672629 2.52289487635229 1.0
3.81580369919009 0.836507460320988 1.0
0.760070734020472 -0.121085766638158 0.0
-0.773776628513234 3.17548701592023 2.0
};
\end{axis}

\end{tikzpicture}

\end{document}

Note: This problem was not a regression of #464, as it persists also in older versions (<= 0.9.6).

Also, I don't know whether opacity/transparency should be introduced in this manner (though it is done in this way when only marks is used/no cmapping is applied). Mabye it is better to explicitly multiply it to the cmapped color inside /scatter/use mapped color (refer #480) ?

For those with the same issue, I could work around using the solution you proposed for #480 (in my case opacity = 0.5).

tikzplotlib.save("test.tex",extra_axis_parameters={"scatter/use mapped color={draw=black,fill=mapped color,opacity=0.5}"})

Looking forward to have a more durable solution to this one :-)