5
votes

I am trying to save a plot into a file using plt.savefig, however I am dissatisfied with the output picture quality. Changing dpi option doesn't help.

plt.savefig('filename.png', dpi=1200, format='png', bbox_inches='tight')

I tried saving to 'svg' and 'eps' - makes no difference. I wonder if the problem is with something else, like version of some library or OS or something alike. It also looks like the problem is not with resolution but the way lines and symbols are drawn - too bold.

plt.show() shows significantly better picture, and I can save it to png with satisfying quality - and surprisingly file size is about 8 times smaller (because of compressing, I suppose, which is fine.)

Part of the picture saved using savefig() enter image description here

The same part of the picture saved from plot.show() enter image description here

2
If you use the same dpi for showing as for saving there should not be any difference in the output. The defaut dpi is 100. Using dpi=100 should give you the same result as for showing. It is hence not clear what you mean by "Changing dpi option doesn't help." The output when using dpi=1200 is expected - there is no problem with that.ImportanceOfBeingErnest
That being said you would probably want to update your question with the result of dpi=100 and explain what you don't like about that.ImportanceOfBeingErnest
The problem is not in the resolution, but rather in the style: picture with higher resolution has less pleasant view. I'll update question title, as it has nothing to do with resolution, really. I just didn't know where to look, it's fixed now.Tatiana Didik
You need to imagine the dpi to act like a magnifying glass, it'll make everything bigger or smaller. In contrast the figure size is the size of the canvas where the elements are plotted. So the solution is probably given in this answer.ImportanceOfBeingErnest
Thank you! Yes, it's a helpful link. Figsize is exactly what I needed.Tatiana Didik

2 Answers

2
votes

Figsize option did the trick for me.

The idea is that default parameters for saving to file and for displaying the chart are different for different devices. That's why representation was different in my case. It's possible to adjust settings manually (as Piotrek suggests), but for me it was enough just to increase figure size - this setting is shared and allows python to auto-adjust visualization.

More details are on the page Piotrek mentioned, answered by doug and Karmel.

I have several subplots, so i used it like that:

fig, ax = plt.subplots(nrows=4, ncols=1, figsize=(20, 10))

For one plot case command is like that:

plt.figure(figsize=(20,10))

P.S. figsize parameters are in inches, not pixels.

1
votes

Have a look here: Styles and Futurile

In short, you can experiment with the following options to edit the line, ticks etc.

plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Ubuntu'
plt.rcParams['font.monospace'] = 'Ubuntu Mono'
plt.rcParams['font.size'] = 10
plt.rcParams['axes.labelsize'] = 10
plt.rcParams['axes.labelweight'] = 'bold'
plt.rcParams['axes.titlesize'] = 10
plt.rcParams['xtick.labelsize'] = 8
plt.rcParams['ytick.labelsize'] = 8
plt.rcParams['legend.fontsize'] = 10
plt.rcParams['figure.titlesize'] = 12

Also have a look at this topic:

matplotlib savefig() plots different from show()