In the middle of the desert you can say anything you want

28 Jun 2023

Everything I know about saving plots in matplotlib, seaborn, plotly, as PNG and vector PDF/EPS etc.

Seaborn saving with correct border

When saving seaborn images there was weirdness going on, with borders either cutting labels or benig too big.


# bad: cut corners
# good: no cut corners and OK bounding box
ax.figure.savefig("inat_pnet_lorenz.png", bbox_inches="tight")

Save as PDF/EPS for better picture quality in papers

EDIT 2023-12-14

Paper reviewer suggested exporting in PDF, which led me to graphics - Good quality images in pdflatex - TeX - LaTeX Stack Exchange:

Both gnuplot and matplotlib can export to vector graphics; file formats for vector graphics are e.g. eps or pdf or svg (there are many more). As you are using pdfLaTeX, you should choose pdf as output format, because it will be easy to include in your document using the graphicx package and the \includegraphics{} command.

Awesome! So I can save to PDF and then include using the usual code (edit - eps works as well). Wow!


Static image export in Python:


PDF works as-is as well, EPS needs the poppler library but then works the same way

For excessive margins in the output PDFs:]

fig.update_layout( margin=dict(l=20, r=20, t=20, b=20), )

Overleaf antialiasing blurry when viewing

When including a PDF plot, I get this sometimes: 2024-01-18-141431_439x363_scrot.png 2024-01-18-141523_261x79_scrot.png

This is a problem only when viewing the PDF inside qutebrowser/Overleaf, in a normal PDF viewer it’s fine!

Nel mezzo del deserto posso dire tutto quello che voglio.