Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "zoom out" a plot in matplotlib, keeping all the size ratios the same, but reducing the size in inches?

Tags:

I'm using matplotlib for plots, and one problem I'm having is that the standard plot sizes appear to have ridiculously huge sizes (in inches). The default dpi value for MPL seems to be set at 100dpi, but plots get produced at sizes of several like 6+ inches. The problem is that if I save this as a pdf, the immediate size is much too large to put on a scientific paper (I'd rather have it be "column-sized" which would be like an inch or two of width).

However, if you directly specify the figsize like

fig, ax = plt.subplots(figsize=[2.,3.])

the font sizes and marker sizes stay the same size in inches, leading to relatively huge figure items and ugly graphics. What I would prefer is the same ratios that come up by default, but reduced to a different inch size.

There's two ways around this that I know of. Either save the figure in the standard size (which is coming out to about 8 by 6 inches or something), and then scale it when inserting it into LaTeX. Or manually go through all the figure elements, and change the font sizes, marker sizes, and figure sizes to make it perfect.

In some cases you can't do the former, and the latter is extremely tedious. I'd have to figure out for each figure element what the proper size is to keep it the same ratio as in the default.

So if there another quick option for changing figure size while also scaling everything from what it would be by default?

like image 482
Marses Avatar asked Sep 08 '17 15:09

Marses


1 Answers

Usually, it would be enough to set a different font-size for the complete script.

import matplotlib.pyplot as plt
plt.rcParams["font.size"] =7

Complete example:

import matplotlib.pyplot as plt
plt.rcParams["font.size"] =7


fig, ax = plt.subplots(figsize=[2.,3.], dpi=100)
ax.plot([2,4,1], label="label")
ax.set_xlabel("xlabel")
ax.set_title("title")
ax.text( 0.5,1.4,"text",)
ax.legend()

plt.tight_layout()
plt.savefig("figure.pdf", dpi="figure")
plt.savefig("figure.png", dpi="figure")
plt.show()

enter image description here enter image description here

like image 78
ImportanceOfBeingErnest Avatar answered Oct 13 '22 18:10

ImportanceOfBeingErnest