0

I'm plotting data with matplotlib pgf backend for inclusion in LaTeX documents. The backend produces a pgf file which is rendered by LaTeX and includes all labels, axis and so on. It also produces png files containing the underlying image, colorbar and so on.

Is it possible to produce these images in pdf or svg or some other vector graphics format for higher quality?

Clarification: The problem of producing vector graphics only arises in combination with the PGF back-end. I want to use the PGF backend so that all text is rendered by LaTeX during compilation of my document. The PGF back-end produces not pure LaTeX/PGF code but some parts of the figure as a PNG image and includes it: \pgfimage[interpolate=true,width=0.126667in,height=2.560000in]{plot.png}. I want these parts to be generated as PDF not PNG. savefig(plot.pgf, fomat='pdf') does not work.

bjoern
  • 13
  • 3
  • 1
    Possible duplicate of [How can I get the output of a matplotlib plot as an SVG?](https://stackoverflow.com/questions/24525111/how-can-i-get-the-output-of-a-matplotlib-plot-as-an-svg) [Save the plots into a PDF](https://stackoverflow.com/questions/11328958/save-the-plots-into-a-pdf)? – ComplicatedPhenomenon Aug 05 '19 at 09:36
  • doc for [matplotlib.pyplot.savefig](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.savefig.html) mentions formats `png`, 'pdf', `svg`. I tried `plot([1,4,2,3])` and then `savefig()` as `output.svg`, `output.pdf` and `output.ps` and all of them gives me vector image which I can open in Inkscape and edit. – furas Aug 05 '19 at 09:44
  • The problem only arises in combination with the PGF backend, therefore this is also not a duplicate. There was one similar question years ago, but without an answer. I want to use the PGF backend so that all text is rendered by LaTeX during compilation of my document. The PGF back-end produces not pure LaTeX/PGF code but some parts of the figure as a PNG image and includes it: `\pgfimage[interpolate=true,width=0.126667in,height=2.560000in]{plot.png}`. I want these parts to be generated as PDF not PNG. `savefig(plot.pgf, fomat='pdf')` does not work. – bjoern Aug 05 '19 at 09:59
  • @furas Please remove the [duplicate] tag. My question is not about producing vector graphics output in general, but from within the pgf back-end for inclusion in the pgf/LaTeX file. It therefore needs to be a vector graphic with exactly the right size without all the things specified in the pgf file/rendered by LaTeX. – bjoern Aug 05 '19 at 11:24
  • Reopened. I don't know how works PGF but maybe you can use `savefig` to generate file svg and manually change line `\pgfimage` to use this file. I never saw PDF as part of other file. – furas Aug 05 '19 at 12:13
  • A PDF image is basically just a png. So it’s not clear what you are trying to achieve here. – Jody Klymak Aug 05 '19 at 14:59
  • @JodyKlymak I want to get a scalable vector graphic for embedding in my LaTeX document. PDF supports raster images (like PNG) and scalable vector graphics, so I don't know why you say that a PDF image is basically just a PNG. – bjoern Aug 14 '19 at 07:09
  • matplotlib rasterizes some objects. Those rasters are what is saved as a png in your pgf file. If you save the same figure as PDF those rasters will still be rasters in the PDF. The user has control over whether some objects are rasterizes or not. But adding PDF to a png won’t particularly help. – Jody Klymak Aug 14 '19 at 14:42
  • Ok, if the objects in the PNG are always rasterized by matplotlib saving them as PDF won't make a difference. Thanks for clearing that up! – bjoern Aug 14 '19 at 17:18

0 Answers0