How to set constant figsize in matplotlib

Viewed 352

I am doing some experimentations with plotting and I like big plots i.e plots on a big picture. So I have to manually do

plt.figure(figsize=(15,8))
myplotcode

every time. I want to know is there anyway that I set this figsize as constant in starting and do not use it manaully everytime.

Thanks

2 Answers

In MatplotLib to can edit the base parameters, which is arranged in a dict form. You can actually change every default parameter. In your case it would like this:

rcParams["figure.figsize"] = [15, 8]

For more parameters you can change, you can check out the documentation.

If you want to set the figure size once and forever you can store you preferences (not only the figure size) in an initialization file, named matplotlibrc

On linux/Unix like systems

$ mkdirhier ~/.config/matplotlib
$ echo "figure.figsize     : 10, 7" >> /home/boffi/.config/matplotlib/matplotlibrc
$ python
...
>>> import matplotlib.pyplot
>>> matplotlib.pyplot.figure()
<Figure size 1000x700 with 0 Axes> 
>>> # the above size is in pixels, at the default of 100 dpi
>>> 

To know the location of the configuration directory you can do

$ python
...
>>> import matplotlib
>>> matplotlib.get_configdir()
'/home/user1001/.config/matplotlib'
>>> 

An initialization file is good for you, but if you want to distribute your script to other people, with different initialization files, well it's no more so good!

A possible remedy is to go with Matplotlib's style sheets that allows you to load all of your tweakings with a single statement

plt.style.use('./mystyle.mplstyle')

and also the possibility of using a style as a context manager

plt.plot(...)         # default style
with plt.style.context('./mystyle.mplstyle'):
     plt.plot(...)    # your style

so that you can distribute your style sheet along with your script and your user will not be affected by a global modification of the defaults.

Related