20

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.

Bowen Peng
  • 1,635
  • 4
  • 21
  • 39

2 Answers2

35

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:

Jason R Stevens CFA
  • 2,232
  • 1
  • 22
  • 28
pktl2k
  • 598
  • 5
  • 12
4

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

W. Ding
  • 587
  • 4
  • 15