Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the default figure size and DPI of all plots drawn by `matplotlib.pyplot`

I know how to set figure size and DPI of one plot by fig, ax = plt.figure(figsize=(8,8), dpi=140).
But I am wondering there is a way that can change the figure size or DPI of all plots without specifying these values each time.
Could anyone help me?
Thanks in advance.

like image 409
Bowen Peng Avatar asked May 21 '19 05:05

Bowen Peng


People also ask

What is default figure size in matplotlib?

If not provided, defaults to rcParams["figure. figsize"] = [6.4, 4.8] .

What is matplotlib default dpi?

resolution of the figure. If not provided, defaults to rcParams["figure. dpi"] (default: 100.0) = 100 .


2 Answers

For your specific case, you probably want to set

import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [8.0, 8.0]
plt.rcParams['figure.dpi'] = 140

This will change the default for this one script.

However, there are a number of ways how you can change the defaults permanently. You could modify the matplotlibrc file or create your own style sheet. Please refer to the matplotlib documentation for details:

  • https://matplotlib.org/tutorials/introductory/customizing.html
  • https://matplotlib.org/users/dflt_style_changes.html
like image 154
pktl2k Avatar answered Sep 17 '22 20:09

pktl2k


1. Apply to a single file

import matplotlib.pyplot as plt
plt.rcParams['savefig.dpi'] = 300
%matplotlib inline

2. Apply to all files

Find ~\Python37\Lib\site-packages\matplotlib\mpl-data\matplotlibrc. Let figure.dpi=300.

This will be overwritten in your next install. Put this file in the following path can avoid being overwritten.

  • Unix/Linux:

$HOME/.config/matplotlib/matplotlibrc

$XDG_CONFIG_HOME/matplotlib/matplotlibrc

  • Other platforms:

$HOME/.matplotlib/matplotlibrc

like image 45
W. Ding Avatar answered Sep 21 '22 20:09

W. Ding