matplotlib.rcParams is a matplotlib.RcParams object, it is a dictionary-like variable which store some rc settings in matplotlib. In this tutorial, we will introduce how to use it.
matplotlib.rcParams
matplotlib.rcParams contains some properties in matplotlibrc file. We can use it to control the defaults of almost every property in Matplotlib: figure size and DPI, line width, color and style, axes, axis and grid properties, text and font properties and so on.
In order to use matplotlib.rcParams, we should know what properties are stored in it, these properties can be foud in matplotlibrc file.
matplotlibrc file
We can use code below to find the path of matplotlibrc file.
import matplotlib f = matplotlib.matplotlib_fname() print(f)
Run this code, we find the path is:
C:\Users\fly165\.conda\envs\py3.6\lib\site-packages\matplotlib\mpl-data\matplotlibrc
We can find Matplotlib configuration are currently divided into following parts:
## - BACKENDS ## - LINES ## - PATCHES ## - HATCHES ## - BOXPLOT ## - FONT ## - TEXT ## - LaTeX ## - AXES ## - DATES ## - TICKS ## - GRIDS ## - LEGEND ## - FIGURE ## - IMAGES ## - CONTOUR PLOTS ## - ERRORBAR PLOTS ## - HISTOGRAM PLOTS ## - SCATTER PLOTS ## - AGG RENDERING ## - PATHS ## - SAVING FIGURES ## - INTERACTIVE KEYMAPS ## - ANIMATION
Here is an example:
How to use matplotlib.rcParams to set the matplotlib configuration?
We will use an example to show you how to do.
import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl # create data x = np.linspace(0, 4*np.pi) y = np.sin(x) mpl.rcParams['figure.figsize'] = (5.0, 4.0) # set figure size mpl.rcParams['image.interpolation'] = 'nearest' # set interpolation mpl.rcParams['font.sans-serif'] = 'SimHei' # set font mpl.rcParams['axes.unicode_minus'] = False mpl.rcParams['lines.linestyle'] = ':' mpl.rcParams['lines.linewidth'] = 3 # draw sine function plt.title('sin function') plt.plot(x, y, label='$sin(x)$') plt.show()
In this code, we will use mpl.rcParams[key] = value to set the matplotlib configuration.
Run this code, you will get the image: