tikzplotlib
tikzplotlib copied to clipboard
Figure does not scale properly between pyplot and latex
I'm finding with my figures that I cannot make the resulting tikz figure look the same as my original pyplot figure.
Take the MWE provided by the creators, with a small edit to the linewidth of the second plot and force the figure to the LaTeX article document column width.
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.figure(figsize=(4.773, 4.773)) # force fig size to article column width
plt.plot(t, s, "o-", lw=8) # linewidth changed
plt.plot(t, s2, "o-", lw=4.1)
plt.xlabel("time (s)")
plt.ylabel("Voltage (mV)")
plt.title("Simple plot $\\frac{\\alpha}{2}$")
plt.grid(True)
import matplotlib2tikz
matplotlib2tikz.save("test.tex", figureheight='4.773in',
figurewidth='4.773in') # add size args to ensure same size fig
plt.savefig("test.pdf") # save as pdf to compare
The plot looks different in the python viewer (and indeed if saved as a PDF) than it does when rendered as a tikz figure in LaTeX; in this case the line is thicker than the markers in pyplot but far from that in the tikz figure (see attachments). The problem with this difference is that I tweak the plot in python, not in LaTeX, so it would be nice to have the confidence that once perfected in python the plot will appear the same in LaTeX.
I found this to be the case with many things, including markers, annotations, line widths. This also applies if I don't try to ensure the same size.
I found, that the tikzimage includes 'scale=0.4' for some nodes. Manually removing this property from the output fixes the issue for me. I guess this line is to blame.
I removed that line in my installation of tikzplotlib and now my text is scaled correctly.