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

Dashed grid not shown correctly

r3v1 opened this issue · comments

Hi,
I tried to plot simple histogram with dashed grid,

import matplotlib.pyplot as plt
import tikzplotlib

x = [1, 2, 3, 3, 2, 2, 4, 5, 2, 2, 3]

# Draw histogram
plt.hist(x, facecolor="g", alpha=0.75)

plt.grid(True, linestyle='--')
plt.ylabel('Counts')

plt.tight_layout()

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

with the next output:
Good plot

% This file was created by tikzplotlib v0.9.9.
\begin{tikzpicture}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmajorgrids,
xmin=0.8, xmax=5.2,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ylabel={Counts},
ymajorgrids,
ymin=0, ymax=5.25,
ytick style={color=black}
]
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:1,0) rectangle (axis cs:1.4,1);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:1.4,0) rectangle (axis cs:1.8,0);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:1.8,0) rectangle (axis cs:2.2,5);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:2.2,0) rectangle (axis cs:2.6,0);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:2.6,0) rectangle (axis cs:3,0);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:3,0) rectangle (axis cs:3.4,3);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:3.4,0) rectangle (axis cs:3.8,0);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:3.8,0) rectangle (axis cs:4.2,1);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:4.2,0) rectangle (axis cs:4.6,0);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:4.6,0) rectangle (axis cs:5,1);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:1,0) rectangle (axis cs:1.4,1);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:1.4,0) rectangle (axis cs:1.8,0);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:1.8,0) rectangle (axis cs:2.2,5);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:2.2,0) rectangle (axis cs:2.6,0);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:2.6,0) rectangle (axis cs:3,0);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:3,0) rectangle (axis cs:3.4,3);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:3.4,0) rectangle (axis cs:3.8,0);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:3.8,0) rectangle (axis cs:4.2,1);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:4.2,0) rectangle (axis cs:4.6,0);
\draw[draw=none,fill=green!50!black,fill opacity=0.75] (axis cs:4.6,0) rectangle (axis cs:5,1);
\end{axis}

\end{tikzpicture}

But when compiling with pdflatex it shows solid linestyles instead of dashed ones:

Bad plot

Hey! Do you know about the strict=True (defaults to False) parameter that you can pass to the tikzplotlib.save function?

Here's what I get with the following code (also added standalone=True to make it easier for me to get a standalone PDF w/ the fig.)

import matplotlib.pyplot as plt
import tikzplotlib

x = [1, 2, 3, 3, 2, 2, 4, 5, 2, 2, 3]
plt.hist(x, facecolor="g", alpha=0.75)
plt.grid(True, linestyle='--')
plt.ylabel('Counts')
plt.tight_layout()
tikzplotlib.save("bad_tex.tex", strict=True, standalone=True)

bad_tex

The grid is not in dashed style and I don't know how to solve that. But the ticks now match the original matplotlib figure.