Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Legend icons are off by one when imshow is used in same axis as lineplots #361

Open
Aikhjarto opened this issue Jan 3, 2020 · 2 comments

Comments

@Aikhjarto
Copy link
Contributor

When combining an image with plt.imshow() and lines with plt.plot(), the legend is off.

Code to produce a axis with an imshow image as well as lineplots (only one marker printed for brevity):

import matplotlib.pyplot as plt
import tikzplotlib
plt.imshow([[1,2],[3,4]]) # no change in outcome if label was also set here
plt.plot(0,0,'x',label='0')
plt.plot(0,1,'x',label='1')
plt.plot(1,0,'x',label='2')
plt.plot(1,1,'x',label='3')
plt.legend()
plt.savefig('test_legend_imshow.png')
tikzplotlib.save('test_legend_imshow.tex',standalone=True, encoding='utf-8')

Resulting tex code:

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=newest}
\DeclareUnicodeCharacter{2212}{−}
\begin{document}
% This file was created by tikzplotlib v0.8.7.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
\definecolor{color1}{rgb}{1,0.498039215686275,0.0549019607843137}
\definecolor{color2}{rgb}{0.172549019607843,0.627450980392157,0.172549019607843}
\definecolor{color3}{rgb}{0.83921568627451,0.152941176470588,0.156862745098039}

\begin{axis}[
legend cell align={left},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
xmin=-0.5, xmax=1.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.01960784313725!black},
ymin=-0.5, ymax=1.5,
ytick style={color=black}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=1.5, ymin=1.5, ymax=-0.5] {test_legend_imshow-000.png};
\addplot [semithick, color0, mark=x, mark size=3, mark options={solid}, only marks]
table {%
0 0
};
\addlegendentry{0}
\addplot [semithick, color1, mark=x, mark size=3, mark options={solid}, only marks]
table {%
0 1
};
\addlegendentry{1}
\addplot [semithick, color2, mark=x, mark size=3, mark options={solid}, only marks]
table {%
1 0
};
\addlegendentry{2}
\addplot [semithick, color3, mark=x, mark size=3, mark options={solid}, only marks]
table {%
1 1
};
\addlegendentry{3}
\end{axis}

\end{tikzpicture}

\end{document}

Expected output from plt.savefig:
test_legend_imshow
Output after compiling pdf with pdflatex and pgfplots 1.16
test_legend_imshow pdf
The labels in the legend are correct, but the icon is shifted by one entry.

@Aikhjarto
Copy link
Contributor Author

The problem persists even after updating to the newest versions of tikzplotlib (0.9.2) and pgfplots (1.17).

@XXjogeXX
Copy link

XXjogeXX commented May 14, 2024

I still encounter the same problem. Is there a solution yet?

A manual fix is to add forget plot to the graphics import
\addplot [forget plot] graphics [...]

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants