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

Label of errorbar incorrectly matched in legend

mb opened this issue · comments

Both descriptions in the leged show the label of the second errorbar:
bug-output + expected output

  • the Python code
import matplotlib.pyplot as plt
import numpy as np

plt.style.use("ggplot")

t = np.arange(0.0, 2.0, 0.1)
s = np.sin(2 * np.pi * t)
s2 = np.cos(2 * np.pi * t)
plt.errorbar(t, s, lw=4.1, label="a") # similar to the example in the README.md, but with errorbar instead of plot
plt.errorbar(t, s2, lw=4.1, label="b") # would work if both are plot
plt.legend()

import tikzplotlib

tikzplotlib.save("test.tex")
plt.show()

Figure_1

  • the output TeX (Pgfplots)
% This file was created by tikzplotlib v0.9.6.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.886274509803922,0.290196078431373,0.2}
\definecolor{color1}{rgb}{0.203921568627451,0.541176470588235,0.741176470588235}

\begin{axis}[
axis background/.style={fill=white!89.8039215686275!black},
axis line style={white},
legend cell align={left},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.91,0.5)}, anchor=east, draw=white!80!black, fill=white!89.8039215686275!black},
tick align=outside,
tick pos=left,
x grid style={white},
xmajorgrids,
xmin=-0.05, xmax=1.05,
xtick style={color=white!33.3333333333333!black},
y grid style={white},
ymajorgrids,
ymin=-0.0500000000000003, ymax=1.05,
ytick style={color=white!33.3333333333333!black}
]
\addplot [line width=1.64pt, color0]
table {%
0 0 
1 -2.44929359829471e-16
};
\addlegendentry{b}
\addplot [line width=1.64pt, color1]
table {%
0 1 
1 1 
};
\addlegendentry{b}
\end{axis}

\end{tikzpicture}
  • the expected TeX (Pgfplots) (diff).
28c28
< \addlegendentry{b}
---
> \addlegendentry{a}
  • the expected TeX (Pgfplots).
% This file was created by tikzplotlib v0.9.6.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.886274509803922,0.290196078431373,0.2}
\definecolor{color1}{rgb}{0.203921568627451,0.541176470588235,0.741176470588235}

\begin{axis}[
axis background/.style={fill=white!89.8039215686275!black},
axis line style={white},
legend cell align={left},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.91,0.5)}, anchor=east, draw=white!80!black, fill=white!89.8039215686275!black},
tick align=outside,
tick pos=left,
x grid style={white},
xmajorgrids,
xmin=-0.05, xmax=1.05,
xtick style={color=white!33.3333333333333!black},
y grid style={white},
ymajorgrids,
ymin=-0.0500000000000003, ymax=1.05,
ytick style={color=white!33.3333333333333!black}
]
\addplot [line width=1.64pt, color0]
table {%
0 0
1 -2.44929359829471e-16
};
\addlegendentry{a}
\addplot [line width=1.64pt, color1]
table {%
0 1
1 1
};
\addlegendentry{b}
\end{axis}

\end{tikzpicture}

I came across the same error and also don't know how to fix it. And I've just moved over from Matlab, and I have brought a workaround with me: Just set the errorbar command to have no markers and no linestyle, then follow the errorbar command with a plot command that makes the markers (if you use them), the line (if you use one) and the legend entries.

To make sure the errorbar commands don't lead to legend entries, give them label='_' or anything that begins with an underscore.

Not a very elegant solution, but a decent workaround.

I think I didn't installed something additionally, so this might work for you too. If you want to include your plot in latex, you can also use plt.savefig('your-plot.pgf') without any additional dependencies.