使用matplotlib绘制折线图
示例1
import matplotlib.pyplot as plt
import numpy as np
# 中文显示
plt.rcParams["font.sans-serif"]=["SimHei"] #设置字体
plt.rcParams["axes.unicode_minus"]=False #该语句解决图像中的“-”负号的乱码问题
font = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 15,
}
x = np.arange(1,9)
y1 = [0.7, 0.7, 0.6, 0.5, 0.5, 0.4, 0.4, 0.4]
y2 = [0.8, 0.7, 0.7, 0.6, 0.5, 0.5, 0.4, 0.4]
y3 = [0.6, 0.6, 0.5, 0.5, 0.4, 0.4, 0.4, 0.3]
y4 = [0.8, 0.8, 0.7, 0.6, 0.5, 0.5, 0.4, 0.4]
#绘制折线图,添加数据点,设置点的大小
# * 表示绘制五角星;此处也可以不设置线条颜色,matplotlib会自动为线条添加不同的颜色
plt.plot(x, y1, color='m', marker='.', markersize=10, linestyle='--', linewidth=2.0, clip_on=False)
plt.plot(x, y2, color='g', marker='.',markersize=10, linestyle='-.', linewidth=2.0, clip_on=False)
plt.plot(x, y3, color='c', marker='.', markersize=10, linestyle=':', linewidth=2.0, clip_on=False)
plt.plot(x, y4, color='r', marker='.',markersize=10, linestyle='-', linewidth=2.0, clip_on=False)
# 设置坐标刻度,区间范围
plt.xticks(np.arange(1, 8.1, 1), fontproperties='Times New Roman', size = 13)
plt.yticks(np.arange(0.3, 1.1, 0.1), fontproperties='Times New Roman', size = 13)
plt.xlim((1, 8))
plt.xlabel("Number of pictures", font) # x轴标题
plt.ylabel("Average RD", font) # y轴标题
#绘制图例
font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 14,
}
plt.legend(['Euclidean', 'Manhattan', "Chebyshev", "Ours"], prop = font1, frameon=False, loc="upper right")
ax = plt.gca() #gca:get current axis得到当前轴
#设置图片的右边框和上边框为不显示
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.show()
示例2
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["font.sans-serif"]=["SimHei"] #设置字体
plt.rcParams["axes.unicode_minus"]=False #该语句解决图像中的“-”负号的乱码问题
font = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 15,
}
#准备绘制数据
x = ['dinosaur', 'build', 'plane', 'car','cat', 'flower', 'train', 'tree', 'landscape', 'wolf']
y1 = [0.9,0.3,0.4,0.4,0.9,0.8,0.6,0.7,0.4,0.8]
y2 = [1,0.3,0.3,0.7,1,0.8,0.5,0.7,0.8,0.8]
plt.plot(x, y1, color='#4F94CD', marker='.', markersize=10, linestyle='--', linewidth=2.0, clip_on=False, label='LBP')
plt.plot(x, y2, color='r', marker='.',markersize=10, linestyle='-', linewidth=2.0, clip_on=False, label='IU-LBP')
plt.ylabel("Average RD", font) # y轴标题
plt.xticks(fontproperties='Times New Roman', size = 13)
plt.yticks(np.arange(0.3, 1.1, 0.1), fontproperties='Times New Roman', size = 13)
font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 14,
}
plt.legend(prop = font1, frameon=False, loc="upper right")
ax = plt.gca() #gca:get current axis得到当前轴
#设置图片的右边框和上边框为不显示
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.show()