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

Manual legend patch is missing

senselessDev opened this issue · comments

Originally, I was imshowing some rgb image and wanted to add a legend explaining what color channel is giving which information. Therefore, I needed to add a manual legend entry via a matplotlib Patch object. In the MWE, that is the only thing I expect to show:

import matplotlib.pyplot
import matplotlib.patches
import tikzplotlib

fig, ax = matplotlib.pyplot.subplots(1)

green_patch = matplotlib.patches.Patch(color='g', label='test patch')
ax.legend(handles=[green_patch])
tikzplotlib.save('test_patch.tex', standalone=True)
matplotlib.pyplot.show()

tikzplotlib seems to just not iterate over those elements in the _save recursion. At least that is what I think is happening from a quick print output in

elif isinstance(child, mpl.patches.Patch):
data, cont = _patch.draw_patch(data, child)
content.extend(cont, child.get_zorder())

where it is going through a certain number of times which does not depend on the patch (so for the MWE it is hitting 2 times - with and without the added patch).
So there's nothing in here:

\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}[
legend cell align={left},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=white!80!black},
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=0, xmax=1,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=0, ymax=1,
ytick style={color=black}
]
\end{axis}

\end{tikzpicture}

\end{document}

test_patch_as_is

whereas something along the line of the following is expected/desired:

\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}[
legend cell align={left},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=white!80!black},
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=0, xmax=1,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=0, ymax=1,
ytick style={color=black}
]
\addlegendimage{area legend, fill=green}
\addlegendentry{test patch}
\end{axis}

\end{tikzpicture}

\end{document}

test_patch_wanted