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

Providing colors explicitly via 2-D array to `scatter` fails

ORippler opened this issue · comments

Description

When providing colors via the c argument in the format of a 2D-array, valid according to matplotlib, the resulting tikz file does not compile.

MWE

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

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

_colors = np.array([(0.2, 0.2, 0.2), (0.8, 0.8, 0.8)])
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='k') # this works

plt.savefig("test.png")

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

Expected result

test

Real result

pdflatex errors with

! Undefined control sequence.
\pgfk@/pgfplots/scatter/@pre marker code/.@cmd ...
                                                  {\noexpand \definecolor {t...
l.442 \end{axis}

This is a regression from #464 , as for tikzplotlib 0.9.6 the snippet above this does not fail.

Tikz code for tikzplotlib 0.9.6:

\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.6.
\begin{tikzpicture}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.231152586529077, xmax=2.89561704167303,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=-0.964413113913117, ymax=2.90972756349348,
ytick style={color=black}
]
\addplot [only marks, mark=*, draw=white!80!black, fill=white!80!black, colormap/viridis]
table{%
x                      y
2.7470395365788 1.8974208681171
0.782990387284591 1.79271455936117
2.07202178558044 2.72713675348149
2.3805188625735 2.06083750824641
0.760070734020472 -0.121085766638158
-0.0825750814348463 0.328478801550698
1.49404639211997 -0.781822303901129
2.22193161637271 2.16683716368713
1.41124187677413 0.320125766693779
2.15653385082545 1.57295213034914
};
\end{axis}

\end{tikzpicture}

\end{document}

Broken Tikz code for tikzplotlib 0.9.8:

\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=-0.231152586529077, xmax=2.89561704167303,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=-0.964413113913117, ymax=2.90972756349348,
ytick style={color=black}
]
\addplot [
  mark=*,
  only marks,
  scatter,
  scatter/@post marker code/.code={%
  \endscope
},
  scatter/@pre marker code/.code={%
  \expanded{%
  \noexpand\definecolor{thispointdrawcolor}{RGB}{\drawcolor}%
  \noexpand\definecolor{thispointfillcolor}{RGB}{\fillcolor}%
  }%
  \scope[draw=thispointdrawcolor, fill=thispointfillcolor]%
},
  visualization depends on={value \thisrow{draw} \as \drawcolor},
  visualization depends on={value \thisrow{fill} \as \fillcolor}
]
table{%
x  y  draw  fill
2.7470395365788 1.8974208681171 204,204,204 204,204,204
0.782990387284591 1.79271455936117 51,51,51 51,51,51
2.07202178558044 2.72713675348149 204,204,204 204,204,204
2.3805188625735 2.06083750824641 204,204,204 204,204,204
0.760070734020472 -0.121085766638158 51,51,51 51,51,51
-0.0825750814348463 0.328478801550698 51,51,51 51,51,51
1.49404639211997 -0.781822303901129 51,51,51 51,51,51
2.22193161637271 2.16683716368713 204,204,204 204,204,204
1.41124187677413 0.320125766693779 51,51,51 51,51,51
2.15653385082545 1.57295213034914 204,204,204 204,204,204
};
\end{axis}

\end{tikzpicture}

\end{document}

Did you manage to solve it somehow? This bug is still present in the last tag (0.9.15).