Package: latexplotlib
latexplotlibPerfect matplotlib figures for latex.Quickstartinstalllatexplotlib:pipinstalllatexplotlibimport latexplotlib and use latexplotlib styleimportlatexplotlibaslpllpl.style.use('latex10pt')# lpl.style.use('latex10pt-minimal')replace all usages ofpltwithlpl. Onlyplt.subplotschanges its behavior:#  fig, axes = plt.subplots(2, 3)fig,axes=lpl.subplots(2,3)Optional:get size of latex document(\the\textwidth,\the\textheight)% (412.123pt, 346.564pt)setlpl.sizeto size of latex documentlpl.size.set(412.123,346.564)UsageThis package has two basic functionalities. On the one hand, it sets sensible defaults
for creating perfect figures for latex. This includes a color scheme optimized for
color-blind people, correct font and font sizes, and sensible defaults to store the
figure. On the other hand, it provides some functions to create perfectly sized figures.
These figures fit your latex document without scaling and have the correct font size for
your document.latexplotlib stylesThere are 6 different styles for matplotlib:latex10pt-minimallatex11pt-minimallatex12pt-minimallatex10ptlatex11ptlatex12ptThe*minimalversions change the font and the font sizes to ensure that the figures fonts match the latex font. This style is fully compatible with other styles:importmatplotlib.pyplotaspltimportnumpyasnpimportlatexplotlibaslpllpl.style.use("latex10pt-minimal")# lpl.size.set(200, 400)withlpl.size.context(200,400):fig,ax=lpl.subplots(1,1)x=np.linspace(1,5,100)fortinrange(4):label=f"$x^{t}$"ax.plot(x,x**t,label=label)ax.set_yscale("log")ax.set_title("Perfect matplotlib figures for\\LaTeX")ax.grid()fig.legend()fig.savefig("example_poly_minimal")fig.savefig("example_poly_minimal.png")The non-minimal versions set additional defaults to create figures that are accessible for color-blind people:importmatplotlib.pyplotaspltimportnumpyasnplpl.style.use("latex10pt")# lpl.size.set(200, 400)withlpl.size.context(200,400):fig,ax=lpl.subplots(1,1)x=np.linspace(1,5,100)fortinrange(4):label=f"$x^{t}$"ax.plot(x,x**t,label=label)ax.set_yscale("log")ax.set_title("Perfect matplotlib figures for\\LaTeX")ax.grid()fig.legend()fig.savefig("example_poly")fig.savefig("example_poly.png")Both styles change the defaults of theplt.savefigcommand. The new defaults arelpl.savefig(...,bbox_inches=None,dpi=300,format="pdf",orientation="portrait",pad_inches=0.05)Get latex dimensionsYou can find the width and height of your document using the following command:\the\textwidth\the\textheightSet and get latex page sizeimportlatexplotlibaslpllpl.size.set(200,400)withlpl.size.context(100,200):lpl.size()# 100, 200lpl.size()# (200, 400)Create figures for lateximportlatexplotlibaslpl# A figure filling 75% of the latex page_=lpl.subplots(1,1)# A subplot filling 80% of the latex pagefig,axes=lpl.subplots(3,2,scale=0.8)# A subplot for 3 square plots next to each otherfig,axes=lpl.subplots(1,3,scale=0.8,aspect='equal')aspectkeywordTheaspectkeyword controls the ratio of height to width. The default is the Golden ratio.aspectcan also beequal(i.e.aspect=1)orauto. In the latter case, the figure fills the available space.importlatexplotlibaslpl# A 3 by 2 figure where each subplots height to width ratio is the golden ratiofig,axes=lpl.subplots(3,2)# A 3 by 2 figure where each subplot having a height to width ratio of 1:1fig,axes=lpl.subplots(3,2,aspect=1.0)# A figure that is exactly 300pt height and 200pt widewithlpl.size.context(200,300):fig,axes=lpl.subplots(3,2,aspect="auto")Include figures in LatexThe most important part of including the figures in latex is to not set the size of the figure using arguments like[width=...]:\includegraphics{test.pdf}Observe that we did NOT adjust the size using arguments like[width=...]:\includegraphics[width=\textwidth]{test.pdf}plt.tight_layout()plt.tight_layout()changes the size of the produced figure. As such it is recommended to not useplt.tight_layout()at all! The same is true forsavefig(..., bbox_inches=None)!Instead all latexplotlib styles usedconstrained_layoutby default.constrained_layouthas a similar functionality compared totight_layout, however it is fully deterministic and does not change the size of the underlying figure.ReferencesThis package is inspired by the following sources:Code:https://pypi.org/project/SciencePlots/Figure sizes:https://jwalton.info/Embed-Publication-Matplotlib-Latex/Color palette (Okabe Ito):https://clauswilke.com/dataviz/color-basics.htmlGolden ratio:https://en.wikipedia.org/wiki/Golden_ratio