Tatiana Goretskaya
Tatiana Goretskaya

Reputation: 576

plt.savefig output image quality

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

Upvotes: 6

Views: 10762

Answers (2)

Tatiana Goretskaya
Tatiana Goretskaya

Reputation: 576

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.

Upvotes: 3

Piotrek
Piotrek

Reputation: 1530

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()

Upvotes: 1

Related Questions