tikzplotlib
tikzplotlib copied to clipboard
Legend missing in bar subplots
trafficstars
I just observed that the legends are missing in any non-initial subplot of bar plots. Here is an MWE:
import matplotlib.pyplot as plt
import tikzplotlib
data = [1, 2, 3, 4, 5]
fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)
ax[0].bar(data, data, label='data')
ax[0].legend()
ax[1].bar(data, data, label='data')
ax[1].legend()
# plt.show() # for preview
tikzplotlib.save('test.tex',
standalone=True)
The output TeX file is
\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 with tikzplotlib v0.10.1.
\begin{tikzpicture}
\definecolor{darkgray176}{RGB}{176,176,176}
\definecolor{lightgray204}{RGB}{204,204,204}
\definecolor{steelblue31119180}{RGB}{31,119,180}
\begin{groupplot}[group style={group size=1 by 2}]
\nextgroupplot[
legend cell align={left},
legend style={
fill opacity=0.8,
draw opacity=1,
text opacity=1,
at={(0.03,0.97)},
anchor=north west,
draw=lightgray204
},
scaled x ticks=manual:{}{\pgfmathparse{#1}},
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xmin=0.36, xmax=5.64,
xtick style={color=black},
xticklabels={},
y grid style={darkgray176},
ymin=0, ymax=5.25,
ytick style={color=black}
]
\draw[draw=none,fill=steelblue31119180] (axis cs:0.6,0) rectangle (axis cs:1.4,1);
\addlegendimage{ybar,ybar legend,draw=none,fill=steelblue31119180}
\addlegendentry{data}
\draw[draw=none,fill=steelblue31119180] (axis cs:1.6,0) rectangle (axis cs:2.4,2);
\draw[draw=none,fill=steelblue31119180] (axis cs:2.6,0) rectangle (axis cs:3.4,3);
\draw[draw=none,fill=steelblue31119180] (axis cs:3.6,0) rectangle (axis cs:4.4,4);
\draw[draw=none,fill=steelblue31119180] (axis cs:4.6,0) rectangle (axis cs:5.4,5);
\nextgroupplot[
legend cell align={left},
legend style={
fill opacity=0.8,
draw opacity=1,
text opacity=1,
at={(0.03,0.97)},
anchor=north west,
draw=lightgray204
},
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xmin=0.36, xmax=5.64,
xtick style={color=black},
y grid style={darkgray176},
ymin=0, ymax=5.25,
ytick style={color=black}
]
\draw[draw=none,fill=steelblue31119180] (axis cs:0.6,0) rectangle (axis cs:1.4,1);
\draw[draw=none,fill=steelblue31119180] (axis cs:1.6,0) rectangle (axis cs:2.4,2);
\draw[draw=none,fill=steelblue31119180] (axis cs:2.6,0) rectangle (axis cs:3.4,3);
\draw[draw=none,fill=steelblue31119180] (axis cs:3.6,0) rectangle (axis cs:4.4,4);
\draw[draw=none,fill=steelblue31119180] (axis cs:4.6,0) rectangle (axis cs:5.4,5);
\end{groupplot}
\end{tikzpicture}
\end{document}
which does not show the legend in the second subplot.
bar_legend_issue.pdf
What I am expecting is like the output of Matplotlib:

Thanks!