需求介绍
在很多时候我们进行数据分析时,我们想要
- 1.让x轴刻度以指定值真实显示,同时又不改变坐标之间的距离。
- 2.让y轴刻度以10的幂次放真实显示,同时又不改变坐标轴之间的距离。
- 3.可以添加图例子,并且可以修改图例大小。
如下图所示。
可以看到的是,
对于y坐标轴
刻度显示为10的*次方,且对应的值为真是值;
对于x坐标轴
刻度以[4,16,64,128]均匀显示,且不影图中点的真实值;
对于图例
其显示在绘制图像的左上方,且自定义设置了显示;
功能实现
1.显示为10的*次方
# 设置y轴刻度,使其以10的幂次方来显示
ax2.set_ylim(10**-2, 10**2)
ax2.set_yscale('log')
2.刻度以指定数字均匀显示
a = [1,2,3,4]
x = [4, 16, 64, 128]
ax2.xaxis.set_ticks(a)
ax2.xaxis.set_ticklabels(x)
3.指定位置画图例
ax2.plot(a, y1, c='mediumseagreen',linewidth=2, label=labelA3C1)
ax2.plot(a, y2, c='cornflowerblue',linewidth=2, label=labelA3C2)
ax2.plot(a, y3, c='indianred',linewidth=2, label=labelM1)
ax2.plot(a, y4, c='orange',linewidth=2, label=labelM2)
# 设置图例字体
plt.legend(prop={'size':17})
完整代码
import numpy as np
import matplotlib.pyplot as plt
# 设置字体
font1 = {
'weight' : 'bold',
'size' : 20,
}
font2 = {
'weight' : 'normal',
'size' : 17,
}
# 设置y轴真实数据
y1_mstar40 = [0.01305954579151038, 0.047451762751229004, 0.14855344623680064, 71.28316590338133]
y2_mstar40 = [0.01110986546573476, 0.03968350526508168, 5.26326487161717, 72.19644612647868]
y1_mstar80 = [0.42301138050178927,0.7442377343134959,1.0093897564152738,2.6024310437774867]
y2_mstar80 = [0.37454420338211175,0.5526457964011748,1.0043409910806163,67.1310858821869]
y1_0il40 = [0.46235645035393225,0.23785825288920215,0.23011677887245272,0.33088768624452636]
y2_0il40 = [0.4208466878537003, 0.24522354217354358,0.2572283843281555,0.39486953573050004]
y1_0il80 = [0.6159738090320918,0.599179550014944,0.5874482524780598,0.7413222459959045]
y2_0il80 = [0.7293902320584796,0.6384663087695188,0.615959735356876,0.9175049519276021]
# 设置x轴名称
xlabel = 'agent number'
# 设置y轴名称
ylabel = 'planning time per target reached[s]'
# 图标名称
title = 'Planning Time Per Target Reached - 40 * 40'
# 设置x轴显示值
# a有四个值,将均匀的显示1.2.3.4四个值
# x内为想要显示的值,即坐标经后面的代码会修改为4.16.64.128
a = [1,2,3,4]
x = [4, 16, 64, 128]
# 创建一张图像为9:6
fig = plt.figure(figsize=(9,6))
# 为图像添加子目标ax2,后续所有的操作为在ax2上操作
ax2 = fig.add_subplot()
# 设置图例名称
labelM1 = 'ODrM* 0.3 density'
labelM2 = 'ODrM* 0.6 density'
labelA3C1 = 'A3C-IL 0.3 density'
labelA3C2 = 'A3C-IL 0.6 density'
# 设置四个y代表的真实数据
y1 = y1_0il40
y2 = y2_0il40
y3 = y1_mstar40
y4 = y2_mstar40
# 添加图例
ax2.plot(a, y1, c='mediumseagreen',linewidth=2, label=labelA3C1)
ax2.plot(a, y2, c='cornflowerblue',linewidth=2, label=labelA3C2)
ax2.plot(a, y3, c='indianred',linewidth=2, label=labelM1)
ax2.plot(a, y4, c='orange',linewidth=2, label=labelM2)
# 设置图例字体
plt.legend(prop={'size':17})
# 分别将y1-y4在图标中画出折线图,a为x四个点的值,y1为y四个点的值,c代表颜色,linewidth代表折线粗细
ax2.plot(a, y1, c='mediumseagreen',linewidth=4)
ax2.plot(a, y2, c='cornflowerblue',linewidth=4)
ax2.plot(a, y3, c='indianred',linewidth=4)
ax2.plot(a, y4, c='orange',linewidth=4)
# 设置x轴,y轴,及图表标题名称及字体
ax2.set_title(title, font1)
ax2.set_xlabel(xlabel, font2)
ax2.set_ylabel(ylabel, font2)
# 将x轴坐标刻度1.2.3.4替换为我们想要的4.16.64.128
ax2.xaxis.set_ticks(a)
ax2.xaxis.set_ticklabels(x)
# 设置y轴刻度,使其以10的幂次方来显示
ax2.set_ylim(10**-2, 10**2)
ax2.set_yscale('log')
# 设置刻度数字大小
ax2.tick_params(which='minor',length=0)
ax2.tick_params(labelsize=14)
# 设置坐标刻度大小
# ax2.tick_params(axis='x',width=1,fontsize=1)
# 画点,将四个点显著标记
ax2.plot(a,y1,'o', markersize=5,color='darkslategray')
ax2.plot(a,y2,'o', markersize=5,color='darkslategray')
ax2.plot(a,y3,'o', markersize=5,color='darkslategray')
ax2.plot(a,y4,'o', markersize=5,color='darkslategray')
# 存储
plt.savefig("./finekk1/compare_time_mstarAndIl_40.jpg")
# 显示
plt.show()