nschloe / tikzplotlib

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

[feature request] layered graphics #347

Open Harry1993 opened 4 years ago

Harry1993 commented 4 years ago

Hello,

I want to reproduce the zorder effects as here. The Python code is pasted as follows:

import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19680801)

x = np.random.random(20)
y = np.random.random(20)

plt.figure()
plt.subplot(211)
plt.plot(x, y, 'C3', lw=3)
plt.scatter(x, y, s=120)
plt.title('Lines on top of dots')

# Scatter plot on top of lines
plt.subplot(212)
plt.plot(x, y, 'C3', zorder=1, lw=3)
plt.scatter(x, y, s=120, zorder=2)
plt.title('Dots on top of lines')
plt.tight_layout()

import tikzplotlib
tikzplotlib.save('./nolayer.tikz')

The LaTeX code:

\documentclass{standalone}

%% tikz's stuff start
\usepackage{tikz, color, graphics, pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}
\usepgfplotslibrary{dateplot}
\usetikzlibrary{patterns, arrows}
%% tikz's stuff end

\begin{document}
\input{./nolayer.tikz}
\end{document}

The expected result is sphx_glr_zorder_demo_001

but tikzplotlib gave me b

where dots are on top of lines in both subplots.

It turns out that the TikZ library does provide layering, therefore we can do a little fine-tuning on the generated .tikz file at this moment, but I think it could be better that tikzplotlib can do this for us in the future.

Thank you, Yanmao

nschloe commented 4 years ago

For my own reference: There's this question on stackoverflow. The reply is not very satisfying though.

I just wrote Christian (the author of pgfplots), so let's see if we're getting proper zorder in pgfplots anytime soon.