nschloe / tikzplotlib

:bar_chart: Save matplotlib figures as TikZ/PGFplots for smooth integration into LaTeX.
MIT License
2.35k stars 205 forks source link

Matplotlib bar plot does not display string xticklabels #440

Open JulianWgs opened 3 years ago

JulianWgs commented 3 years ago

When using a normal bar plot from matplotlib the string xticklabels are not converted, but instead the integer value.

import matplotlib.pyplot as plt

plt.bar(["A", "B"], [1, 2])
print(tikzplotlib.get_tikz_code())
% This file was created by tikzplotlib v0.9.4.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.00392156862745098,0.450980392156863,0.698039215686274}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.49, xmax=1.49,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=0, ymax=2.1,
ytick style={color=black}
]
\draw[draw=none,fill=color0] (axis cs:-0.4,0) rectangle (axis cs:0.4,1);
\draw[draw=none,fill=color0] (axis cs:0.6,0) rectangle (axis cs:1.4,2);
\end{axis}

\end{tikzpicture}

Workaround or correct code:

import pandas as pd
pd.Series([1, 2], ["A", "B"]).plot.bar()
print(tikzplotlib.get_tikz_code())
% This file was created by tikzplotlib v0.9.4.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.00392156862745098,0.450980392156863,0.698039215686274}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.5, xmax=1.5,
xtick style={color=black},
xtick={0,1},
xticklabel style = {rotate=90.0},
xticklabels={A,B},
y grid style={white!69.0196078431373!black},
ymin=0, ymax=2.1,
ytick style={color=black}
]
\draw[draw=none,fill=color0] (axis cs:-0.25,0) rectangle (axis cs:0.25,1);
\addlegendimage{ybar,ybar legend,draw=none,fill=color0};
\addlegendentry{None}

\draw[draw=none,fill=color0] (axis cs:0.75,0) rectangle (axis cs:1.25,2);
\end{axis}

\end{tikzpicture}
ThiloKr commented 3 years ago

I can confirm this issue also for boxplots.