双坐标柱状图
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)
![【python学习】matplotlib绘制双坐标柱状图及柱线图_第1张图片](http://img.e-com-net.com/image/info8/d08308ec1760417699d8d176367784fc.jpg)
图形美化
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)
![【python学习】matplotlib绘制双坐标柱状图及柱线图_第2张图片](http://img.e-com-net.com/image/info8/7f89fb92aa874243b0e12545273a1a67.jpg)
双坐标柱线图
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')
![【python学习】matplotlib绘制双坐标柱状图及柱线图_第3张图片](http://img.e-com-net.com/image/info8/18718599732643d0b38066bd1683c3ac.jpg)
图形美化
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)
![【python学习】matplotlib绘制双坐标柱状图及柱线图_第4张图片](http://img.e-com-net.com/image/info8/4fba210cb9914dab89ae065b0eb434bf.jpg)