python 之 matplotlib 作图
程序员文章站
2022-03-20 11:19:26
...
Python 之 matplotlib 作图
程序如下,这是我论文里的一个例子,画的是几个折线图,特点如下:
1,双坐标系
2,双排legend
3,5种颜色
4,10个maker
5,自定义字体
6 ,科学计数法
font = {'family': 'serif', # 这里是自定义的字体
'color': 'black',
'weight': 'normal',
'size': 14,
}
####plot the cost time.########
Len = np.shape(costTimeAverage)
X =np.array([20,25,30,35,40,45,50,55],dtype=np.float32)
markers =['o','*','^','8','p','s','d','h','x','+'] #定义了10个maker
colors =['red','blue','green','black', 'magenta','sienna','brown','darkred','indigo','midnightblue'] #定义了10种颜色
labels =['ES','MAC-HZE','MAC-SHZE','MAC-SZE','MAC-SSZE'] #5种线型的label。
fig1, ax = plt.subplots(1, 1) #这样可以对坐标轴进行处理
for n in range(5):
plt.plot(X , costTimeAverage[0:8, n], marker = markers[n],color=colors[n], label=labels[n])
plt.xlim([20,55]) #限制x轴的范围
plt.ylim([0,250000]) #限制y轴的范围
plt.legend() #给图加上图例
plt.ylabel("classification time $(ms)$", fontdict=font) #加上y轴的说明。
plt.xlabel("$\sigma$",fontdict=font) #加上x轴的说明。
formatter = ticker.ScalarFormatter(useMathText=True) #
formatter.set_scientific(True) #使用科学计数法
formatter.set_powerlimits((-1,1))
ax.yaxis.set_major_formatter(formatter)
plt.xticks(X)
"""
fig2 =plt.figure()
for n in range(5):
#plt.bar(X+n*0.2,costTimeAverage[:,n],width=0.2,bottom=0,color =colors[n],label=labels[n])
plt.plot(X , costTimeAverage[8:16, n], marker = markers[n], color=colors[n], label=labels[n])
plt.legend()
plt.xlim([20,55])
plt.ylabel("classification time (ms)", fontdict=font)
plt.xlabel("$\sigma$",fontdict=font)
plt.xticks(X)
"""
#####plot the error#########
labels =['ES($\delta_1$)','MAC-HZE($\delta_1$)','MAC-SHZE($\delta_1$)','MAC-SZE($\delta_1$)','MAC-SSZE($\delta_1$)',
'ES($\delta_2$)','MAC-HZE($\delta_2$)','MAC-SZE($\delta_2$)','MAC-SZE($\delta_2$)','MAC-SSZE($\delta_2$)']
labels1 =['ES($\delta_1$)','MAC-HZE($\delta_1$)','MAC-SHZE($\delta_1$)','MAC-SZE($\delta_1$)','MAC-SSZE($\delta_1$)']
labels2 =['ES($\delta_2$)','MAC-HZE($\delta_2$)','MAC-SZE($\delta_2$)','MAC-SZE($\delta_2$)','MAC-SSZE($\delta_2$)']
fig2,ax2 = plt.subplots()
for n in range(10):
if n<3:
plt.plot(X,moreThanT[0:8,n],marker = markers[n],color =colors[n],label=labels[n])
if n>=3 and n<5:
plt.plot(X, moreThanT[0:8, n%2], marker=markers[n], color=colors[n], label=labels[n])
if n>=5 and n<8:
plt.plot(X, lessThanT[0:8, n%5], marker=markers[n], color=colors[n%5], label=labels[n])
if n>=8:
plt.plot(X, lessThanT[0:8, n % 7], marker=markers[n], color=colors[n % 5], label=labels[n])
plt.legend(labels,ncol =2)
ax2.set_ylim([0.9,1.1])
ax2.set_xlim([20,55])
ax2.set_ylabel("actual reliability $\delta_1$", fontdict=font)
ax2.set_xlabel("number of categories",fontdict=font)
ax3 =ax2.twinx() #画双y轴的语句。
ax3.set_ylim([0.9,1.1])
ax3.set_xlim([20,55])
ax3.set_ylabel("actual reliability $\delta_2$", fontdict=font)
ax3.set_xlabel("$\sigma$",fontdict=font)
fig3,ax2 = plt.subplots()
for n in range(5):
if n<3:
plt.plot(X,moreThanT[0:8,n],marker = markers[n],color =colors[n],label=labels[n])
if n>=3 and n<5:
plt.plot(X, moreThanT[0:8, n%2], marker=markers[n], color=colors[n], label=labels[n])
plt.legend(labels1,ncol =2)
ax2.set_ylim([0.9,1.1])
ax2.set_xlim([20,55])
ax2.set_ylabel("actual reliability $\delta_1$", fontdict=font)
ax2.set_xlabel("$\sigma$",fontdict=font)
fig4,ax2 = plt.subplots()
for n in range(5):
if n < 3:
plt.plot(X, lessThanT[0:8, n], marker=markers[n], color=colors[n], label=labels[n])
if n >= 3 and n < 5:
plt.plot(X, lessThanT[0:8, n % 2], marker=markers[n], color=colors[n], label=labels[n])
plt.legend(labels2,ncol =2)
ax2.set_ylim([0.9,1.1])
ax2.set_xlim([20,55])
ax2.set_ylabel("actual reliability $\delta_2$", fontdict=font)
ax2.set_xlabel("$\sigma$",fontdict=font)
plt.show() 显示图形。
经过这次画图,深感Python的强大,一个开源社区维护的语言已经强大到了matlab。至少在我使用的范围内。
Python加油,自己加油,fight!!!