Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib savefig() plots different from show()

When I use show() to plot the graphs in X, the graphs looks very good. However when I start to use savefig() to generate large amount of graphs, the savefig() generated graphs ' font, lines, polygons all look smaller than the show() generated graph. My environment is Ubuntu and the backend for show() is Qt4Agg. How can I make the show() plot and the savefig() plot looks consistent?

like image 999
leon Avatar asked Oct 26 '11 17:10

leon


People also ask

Is PLT show () necessary?

Plotting from an IPython shell draw() . Using plt. show() in Matplotlib mode is not required.

How do I use Savefig in matplotlib?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

Is PLT show () blocking?

Answer #1: show() (not with block=False ) and, most importantly, plt. pause(. 001) (or whatever time you want). The pause is needed because the GUI events happen while the main code is sleeping, including drawing.


2 Answers

savefig specifies the DPI for the saved figure (The default is 100 if it's not specified in your .matplotlibrc, have a look at the dpi kwarg to savefig). It doesn't inheret it from the DPI of the original figure.

The DPI affects the relative size of the text and width of the stroke on lines, etc. If you want things to look identical, then pass fig.dpi to fig.savefig.

E.g.

import matplotlib.pyplot as plt  fig = plt.figure() plt.plot(range(10)) fig.savefig('temp.png', dpi=fig.dpi) 
like image 124
Joe Kington Avatar answered Oct 16 '22 08:10

Joe Kington


You render your matplotlib plots to different devices (e.g., on-screen via Quartz versus to to-file via pdf using different functions (plot versus savefig) whose parameters are nearly the same, yet the default values for those parameters are not the same for both functions.

Put another way, the savefig default parameters are different from the default display parameters.

Aligning them is simple if you do it in the matplotlib config file. The template file is included with the source package, and named matplotlibrc.template. If you did not create one when you installed matplotlib, you can get this template from the matplotlib source, or from the matplotlib website.

Once you have customized this file the way you want, rename it to matplotlibrc (no extension) and save it to the directory .matplotlib (note the leading '.') which should be in your home directory.

The config parameters for saving figures begins at about line 314 in the supplied matplotlibrc.template (first line before this section is: ### SAVING FIGURES).

In particular, you will want to look at these:

savefig.dpi       : 100         # figure dots per inch savefig.facecolor : white       # figure facecolor when saving savefig.edgecolor : white       # figure edgecolor when saving savefig.extension : auto        # what extension to use for savefig('foo'), or 'auto' 

Below these lines are the settings for font type and various image format-specific parameters.

These same parameters for display, i.e., PLT.show(), begin at about line 277 a in the matplotlibrc.template (this section preceded with the line: ### FIGURE):

figure.figsize   : 8, 6           figure.dpi       : 80             figure.facecolor : 0.75        figure.edgecolor : white      

As you can see by comparing the values of these two blocks of parameters, the default settings for the same figure attribute are different for savefig versus display (show).

like image 29
doug Avatar answered Oct 16 '22 07:10

doug