?需要注意的是,由于默认的Pyplot字体并不支持中文符的显示,因此需要通过设置font.sans-serif参数改变绘图时的字体,使得图形可以正常显示中文。同时,由于更换字体后,会导致坐标轴中的部分字符无法显示,因此需要同时更改axes.unicode_minus参数。
plt.rcParams['font.family'] = ['SimHei'] #用来显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示符号
如果需要在坐标轴上显示时间,可以利用DateFormatter提供的功能进行设置,常用代码如下:
from matplotlib.dates import DateFormatter
plt.gca().xaxis.set_major_formatter(DateFormatter('%y/%m/%d'))
#自动旋转X轴的刻度,适应坐标轴
plt.gcf().autofmt_xdate()
除了设置线条的字体的rc参数外,还有设置文本、箱线图、坐标轴、刻度、图例、标记、图片、图像保存等rc参数。
例1.rc参数设置例1
import numpy as np
import matplotlib.pyplot as plt
fig,ax = plt.subplots()
#配置中文显示
plt.rcParams['font.family'] = ['SimHei'] #用来显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示符号
def f(t):
return np.cos(2*np.pi*t)
x1 = np.arange(0.0,4.0,0.5)
x2 = np.arange(0.0,4.0,0.01)
plt.figure(1)
plt.subplot(2,2,1)
plt.plot(x1,f(x1),'bo',x2,f(x2),'k')
plt.title('子图1')
plt.subplot(2,2,2)
plt.plot(x2,f(x2),'r--')
plt.title('子图2')
plt.show()
OUT:
?
例2.rc参数设置例2
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = 'o',label = 'one')
ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = '+',label = 'two')
ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = 'v',label = 'three')
ax.legend(loc = 'best')
OUT:
可以用set_xticks设置X轴刻度
例3.用set_xticks设置刻度
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = 'o',label = 'one')
ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = '+',label = 'two')
ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = 'v',label = 'three')
ax.set_xticks([0,5,10,15,20,25,30,35])
ax.legend(loc = 'best')
OUT:
可以用set_xticklabels改变刻度,设置刻度的旋转角度及字体等。
例4.用set_xticklabels改变刻度
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = 'o',label = 'one')
ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = '+',label = 'two')
ax.plot(np.random.randn(30).cumsum(),color = 'k',linestyle = 'dashed',marker = 'v',label = 'three')
ax.set_xticklabels(['x1','x2','x3','x4','x5'],rotation = 30,fontsize = 'large')
ax.legend(loc = 'best')
OUT:
?其中,rotation参数表示X坐标标签的旋转角度;fontsize为字号,可以取值为“xx-small”“x-small”“small”“medium”“large”“x-large”“xx-large”“smaller”“None”。
?3.绘图的填充
3.1调用函数fill_between()实现曲线下面部分的填充
?
x = np.linspace(0,1,500)
y = np.sin(3*np.pi*x)*np.exp(-4*x)
fig,ax = plt.subplots()
plt.plot(x,y)
plt.fill_between(x,0,y,facecolor = 'green',alpha = 0.3)
?其中,参数x表示整个X轴都覆盖;0表示覆盖的下限;y表示覆盖的上限时y这条曲线,facecolor表示覆盖区域的颜色;alpha表示覆盖区域的透明度[0,1],其值越大,表示越不透明
3.2 部分区域填充
?
plt.fill_between(x[15:300],0,0.4,facecolor = 'green',alpha = 0.3)
3.3 两条曲线之间的区域填充
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,1,500)
y1 = np.sin(3*np.pi*x)*np.exp(-4*x)
y2 = y1 + 0.2
plt.plot(x,y1,'b')
plt.plot(x,y2,'r')
plt.fill_between(x,y1,y2,facecolor = 'green',alpha = 0.3)
plt.show()
?
?
3.4 直接使用fill进行绘图的填充
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,1,500)
y = np.sin(3*np.pi*x)*np.exp(-4*x)
fig,ax = plt.subplots()
ax.fill(x,y)
plt.show()
?