1

I am generating a figure using the PS backend:

import matplotlib
matplotlib.use('ps')
from matplotlib import pyplot as plt
plt.plot((1, 2))

I would like to save the figure:

plt.savefig('test.eps', format='eps', bbox_inches='tight', pad_inches=1.0)

Incidentally, I will make a PNG out of it later (in case it turns out to be relevant to the solution):

 gs -q -dSAFER -dBATCH -dNOPAUSE -dUseTrimBox -dEPSCrop -sDEVICE=pngalpha -sOutputFile=test.png test.eps

The result is (PNG viewed in eog):

png

I would like to change the size of the EPS or the PNG figure, but adding a dpi kwarg to savefig does not change the output. Calling plt.gcf().set_size_inches(10, 10) does nothing as well. How do I make my EPS or PNG file have a higher resolution?

This answer is relevant in that it hints that setting dpi for the ps backend may not be the way to go.

Update

For clarification, it is necessary for me to use the PS backend. At the moment, it is the only one that correctly renders LaTeX colored strings. PGF may or may not do what I want, but I do not have time to research its quirks in depth. I was able to get a hacky solution by adding a -r... argument to gs, but would prefer to control everything from MatPlotLib.

Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264

1 Answers1

1

If you create the figure with plt.figure first, you can give the figsize-kwarg. An example:

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10, 1))  # figsize in inches
fig.show() # if running in ipython
# plt.show() # if running as a script with "python script_name.py"

To save with a specific dpi, it should be enough with mentioned dpi-kwarg to the figures savefig-method, i.e.

fig.savefig(..., dpi=300)

Note that if you work with plt to save the figure or to plot, it will always work with the active figure, which might not be the figure you expect if you happen to have several of them opened.

UPDATE

1) Based on the comment by Mad Physicist, the dpi-kwarg apperently does not work with the PS backend. I do not know why.

pathoren
  • 1,634
  • 2
  • 14
  • 22
  • Thank you very very much for this. You are absolutely right about `gcf()` not doing what I expect it to be doing, even though I ran exactly the code I have posted. – Mad Physicist Aug 04 '16 at 18:26
  • I would like to point out that PS still ignores the `dpi` argument (so you may want to mention that in your answer). It does not actually matter to me though, since the size in inches is what I want. Thanks again. – Mad Physicist Aug 04 '16 at 18:28
  • Ok good that it worked without the dpi, I do not know why it doesnt react on it. I updated the answer, thank you for pointing it out! – pathoren Aug 04 '16 at 19:51
  • I disagree with point 2. `fig.show()` will work almost identically with `plt.show` in both Python and IPython if an interactive backend is used. `ps` is not an interactive backend, so no rendering can be done with it. – Mad Physicist Aug 04 '16 at 20:37
  • Yes you're right. I oversaw that, since I'm usually always using an interactive backend for my applications. – pathoren Aug 04 '16 at 20:49