python matplotlib notes: configuration management
Common configuration items:
axes: Set the color of the axis boundary and surface, axis scale, and grid display;
xticks, yticks: Set the color, size, spacing, direction, label size, etc. for the major and minor ticks of the x-axis and y-axis;
backend: Set the TkAgg and GTKAgg of the target output;
Figure: Control dpi, border color, figure size and subplot settings;
font: font family, font size, style settings;
grid: Set grid color and line style;
legend: Set the display of the legend and its text;
line: Set lines (color, line type, width, etc.) and markers;
Patch: Graphic objects that fill 2D space, such as polygons and circles, control line width, color, anti-aliasing settings, etc.;
savefig: You can make individual settings for saved graphics, such as setting the background of the rendered file to white, etc.;
text: Set font color, text parsing, etc.;
verbose: Set the information output of matplotlib during execution, such as silent, helpful, etc.;
2. Modify the default configuration
matplotlib.rc(group, **kwargs):
set the current rcParams.
group is a grouping of rc, for example, for lines.linewidth, the grouping is lines, for axes.facecolor, the grouping is axes, and so on.
group can also be a list or tuple of group names, such as (xtick, ytick).
kwargs is a dictionary of attribute name/value pairs.
Two usages:
rc('lines', linewidth=2, color='r')
another kind:
rcParams['lines.linewidth'] = 2
rcParams['lines.color'] = 'r'
Expand the first way of writing:
font = {
'family' : 'monospace',
'weight' : 'bold',
'size' : 'larger'}
rc('font', **font) # pass in the font dict as kwargs
The following aliases can be used to save interactive user input:
Alias | Property |
---|---|
‘lw’ | ‘linewidth’ |
‘ls’ | 'line style' |
‘c’ | ‘color’ |
‘fc’ | ‘facecolor’ |
‘ec’ | ‘edgecolor’ |
‘mew’ | ‘markeredgewidth’ |
‘aa’ | ‘antialiased’ |
rc_context:
Returns a context manager for temporarily changing rcParams
with mpl.rc_context({
'interactive': False}):
fig, ax = plt.subplots()
ax.plot(range(3), range(3))
fig.savefig('example.png')
plt.close(fig)
rcdefaults:
Restore the setting of rcParams from Matplotlib's internal default styles.
Those defined in matplotlib.style.core.STYLE_BLACKLIST will not be updated;
matplotlib.rcdefaults()
Other configuration methods include:
# 从 Matplotlib 的内部默认样式恢复
matplotlib.rc_file_defaults()
# 从文件更新。
matplotlib.rc_file(fname, *, use_default_template=True)
# 从 Matplotlib 加载的原始 rc 文件中恢复rc_params
matplotlib.rc_params(fail_on_error=False)
# 从文件fname中读取 配置列表
matplotlib.rc_params_from_file(fname, fail_on_error=False, use_default_template=True)
# 获取配置文件的位置。
matplotlib.get_configdir()
# Get the location of the config file.
matplotlib.matplotlib_fname()
# 返回 Matplotlib 数据的路径。
matplotlib.get_data_path()