matplotlib.pyplot.rc()函数
Matplotlib是Python中一个非常棒的二维数组绘图可视化库。Matplotlib是一个基于NumPy数组构建的多平台数据可视化库,用于更广泛的SciPy堆栈。
matplotlib.pyplot.rc()
matplotlib.pyplot.rc()函数用于设置rc参数。在rc中分组是通过“组”完成的(例如,行)。对于轴中的线,组为线宽。坐标轴的组是facecolor等等。列表或元组也可以作为组名(例如,xtick, ytick)。Kwargs充当名值对,广义上是一个字典,例如:
语法:
rc(‘lines’, linewidth=3, color=’g’)
它设置当前的rc参数:
rcParams[‘lines.linewidth’] = 3
rcParams[‘lines.color’] = ‘g’
为节省交互使用者的输入,现提供下列别名:
Alias | Property |
---|---|
‘lw’ | ‘linewidth’ |
‘ls’ | ‘linestyle’ |
‘c’ | ‘color’ |
‘fc’ | “facecolor” |
‘ec’ | “edgecolor” |
‘mew’ | “markeredgewidth” |
‘aa’ | ‘antialiased’ |
因此可以将上述rc命令缩写如下
rc(‘lines’, lw=3, c=’g’)
注意:可以使用python的kwargs字典来存储默认参数的字典。例如,
font = {‘family’ : ‘monospace’,
‘weight’ : ‘italic’,
‘size’ : ‘medium’}
# pass in the font dict as kwargs
rc(‘font’, **font)
这有助于在不同的配置之间轻松切换。你也可以使用matplotlib.style.use(‘ default ‘)或rcdefaults()来恢复更改后的rc参数。
示例1
from cycler import cycler
import numpy as np
import matplotlib.pyplot as plt
# setting up a custom cycler
sample_cycler = (cycler(color =['r', 'g',
'b', 'y']) +
cycler(lw =[1, 2, 3, 4]))
# using the rc function
plt.rc('lines', linewidth = 4)
plt.rc('axes', prop_cycle = sample_cycler)
A = np.linspace(0, 2 * np.pi, 50)
line_offsets = np.linspace(0, 2 * np.pi, 4,
endpoint = False)
B = np.transpose([np.sin(A + phi) for phi in line_offsets])
figure, (axes0, axes1) = plt.subplots(nrows = 2)
axes0.plot(B)
axes0.set_title('Set default color cycle to 1st plot')
axes1.set_prop_cycle(sample_cycler)
axes1.plot(B)
axes1.set_title('Set axes color cycle to 2nd plot')
# Adding space between the two plots.
figure.subplots_adjust(hspace = 0.4)
plt.show()
输出:
示例2
import matplotlib.pyplot as plt
plt.subplot(332)
plt.plot([1, 2, 3, 4])
# setting the axes attributes
# before the call to subplot
plt.rc('font', weight ='bold')
plt.rc('xtick.major', size = 5, pad = 7)
plt.rc('xtick', labelsize = 15)
# setting aliases for color, linestyle
# and linewidth; gray, solid, thick
plt.rc('grid', c ='0.3', ls ='-', lw = 4)
plt.rc('lines', lw = 2, color ='g')
plt.subplot(312)
plt.plot([1, 2, 3, 4])
plt.grid(True)
# set changes to default value
plt.rcdefaults()
plt.subplot(313)
plt.plot([1, 2, 3, 4])
plt.grid(True)
plt.show()
输出: