【python学习】matplotlib绘制双坐标柱状图及柱线图
本文介绍双坐标柱状图及双坐标柱线图的绘制
·
双坐标柱状图
import matplotlib.pyplot as plt
x=['time1','time2','time3','time4','time5']
y1=[1,2,3,4,5]
y2=[0.1,0.5,1,2,3]
fig=plt.figure()
ax1 = fig.add_subplot(111)
ax1.bar(x,y1,color='lightgray')
ax2 = ax1.twinx()
ax2.bar(x,y2)
图形美化
import matplotlib.pyplot as plt
x=['time1','time2','time3','time4','time5']
y1=[1,2,3,4,5]
y2=[0.1,0.5,1,2,3]
fig=plt.figure(dpi=300)
font1 = {'family' : 'Arial',
'weight' : 'normal',
'size' : 16,
}
ax1 = fig.add_subplot(111)
ax1.bar(x,y1,label='y1',color='lightgray')#绘制柱状图
plt.legend(frameon=False,fontsize='large',bbox_to_anchor=(0.5, 1.02), loc=3, borderaxespad=0)
plt.xlabel('time',font1)
plt.ylabel('test',font1)
plt.xticks(rotation=90,fontsize=12)#调整刻度数值显示角度
plt.yticks(fontsize=12)
ax2 = ax1.twinx()
ax2.bar(x,y2,label='y2',color='tab:pink')
plt.legend(frameon=False,fontsize='large',bbox_to_anchor=(0.02, 1.02), loc=3, borderaxespad=0)
plt.xlabel('time',font1)
plt.ylabel('y2',font1)
plt.xticks(rotation=90,fontsize=12)
plt.yticks(fontsize=12)
双坐标柱线图
x=['time1','time2','time3','time4','time5']
y1=[1,2,3,4,5]
y2=[0.1,0.5,1,2,3]
fig=plt.figure()
ax1 = fig.add_subplot(111)
ax1.bar(x,y1,color='lightgray')
ax2 = ax1.twinx()
ax2.plot(x,y2,linestyle='dotted',color='tab:blue')
图形美化
x=['time1','time2','time3','time4','time5']
y1=[1,2,3,4,5]
y2=[0.1,0.5,1,2,3]
fig=plt.figure(dpi=300)
font1 = {'family' : 'Arial',
'weight' : 'normal',
'size' : 16,
}
ax1 = fig.add_subplot(111)
ax1.bar(x,y1,label='y1',color='lightgray')
plt.legend(frameon=False,fontsize='large',bbox_to_anchor=(0.5, 1.02), loc=3, borderaxespad=0)
plt.xlabel('time',font1)
plt.ylabel('y1',font1)
plt.xticks(rotation=90,fontsize=12)
plt.yticks(fontsize=12)
ax2 = ax1.twinx()
ax2.plot(x,y2,linestyle='dotted',label='y2',color='tab:blue')
plt.legend(frameon=False,fontsize='large',bbox_to_anchor=(0.02, 1.02), loc=3, borderaxespad=0)
plt.xlabel('time',font1)
plt.ylabel('y2',font1)
plt.xticks(rotation=90,fontsize=12)
plt.yticks(fontsize=12)
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献6条内容
所有评论(0)