投稿中文期刊时需要用中文写图例和标题等,在matplotlib中可以设置汉显:
plt.rcParams['font.family'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
举例:
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)*2 + t
x1 = np.arange(0.0, 3.0, 0.5) #设置散点横坐标
x2 = np.arange(0.0, 4.0, 0.001)
#plt.figure(1)
plt.subplot(2,2,1)
plt.plot(x1,f(x1),'bo',x2,f(x2),'b') #先打散点,后画蓝色曲线
plt.title('子图1')
plt.subplot(2,2,2)
plt.plot(np.cos(2*np.pi*x2),'r--')
plt.title('子图2')
plt.show()
其中,在绘制图形的过程中可以为曲线添加散点并修改线型和颜色:
举例:
为了便于观察,先将步长进行修改:
x2 = np.arange(0.0, 4.0, 0.001)
设置线型:
plt.plot(x2,f(x2),color = 'y',linestyle = 'dashed',marker = 'o', label = 'one')
plt.plot(x2,f(x2),color = 'k',linestyle = 'dashed',marker = '+', label = 'three')
plt.plot(x2,f(x2),color = 'k',linestyle = 'dashed',marker = 'v', label = 'three')