14

In matplotlib, I am using LineCollection to draw and color the countries, where the boundaries of the counties are given. When I am saving the figure as a pdf file:

fig.savefig('filename.pdf',dpi=300)

the figure size are quite big. However, on saving them as png file:

fig.savefig('filename.png',dpi=300)

and then converting them to pdf using linux convert command the files are small. I tried reducing the dpi, however that do not change the pdf file size. Is there a way the figures can be saved directly as smaller-pdf files from matplotlib?

imsc
  • 7,492
  • 7
  • 47
  • 69
  • 2
    I found this blog post helpful: http://www.astrobetter.com/blog/2014/01/17/slim-down-your-bloated-graphics/ – Yibo Yang Jan 02 '18 at 10:01

2 Answers2

34

The PDF is larger, since it contains all the vector information. By saving a PNG, you produce a rasterized image. It seems that in your case, you can produce a smaller PDF by rasterizing the plot directly:

plt.plot(x, y, 'r-', rasterized=True)

Here, x, y are some plot coordinates. You basically have to use the additionally keyword argument raterized to achieve the effect.

David Zwicker
  • 23,581
  • 6
  • 62
  • 77
  • 1
    Thanks. I tried using lines = LineCollection(...), lines.set_rasterized(True). Although this reduces the figure size considerably (800kb from 3mb), is still bigger then the png-converted file (400kb). – imsc May 21 '12 at 14:41
  • Did you try setting the dpi of the figure to a lower value, when you create it, i.e. `fig = plt.figure(dpi=100)` – David Zwicker May 21 '12 at 14:44
  • Yes. Both the pdf files (directly saved as well as converted from png) are saved with dpi=300. If I use dpi=100, the figure size decreases but the quality is lower then the png one. – imsc May 21 '12 at 14:58
  • 1
    Could you probably supply a working example, such that we can play around with the settings? Are there other artists, which might profit from rasterizing? – David Zwicker May 21 '12 at 15:07
  • Bit difficult as the datafile I am using are big. If the curves are smooth enough both the png and the pdf files are of same size after rasterizing. However I guess, as in my case the curves are coastlines (zigzag data) the pdf and the png are of different size. Your answer is very close to what I want. Thanks again. – imsc May 21 '12 at 16:01
  • You can also set the rasterization order, as indicated in http://stackoverflow.com/questions/37020842/reducing-size-of-vectorized-contourplot – Løiten Oct 25 '16 at 12:39
3

I think using "rasterized = True" effectively saves the image similarly to png format. When you zoom in, you will see blurring pixels.

If you want the figures to be high quality, my suggestion is to sample from the data and make a plot. The pdf file size is roughly the amount of data points it need to remember.

user108372
  • 171
  • 1
  • 3
  • 9