双y轴坐标轴图
今天利用matplotlib绘图,想要完成一个双坐标格式的图。
fig=plt.figure(figsize=(20,15))
ax1=fig.add_subplot(111)
ax1.plot(demo0719['TPS'],'b-',label='TPS',linewidth=2)
ax2=ax1.twinx()#这是双坐标关键一步
ax2.plot(demo0719['successRate']*100,'r-',label='successRate',linewidth=2)
横坐标设置时间间隔
import matplotlib.dates as mdate
ax1.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d %H:%M:%S'))#设置时间标签显示格式
plt.xticks(pd.date_range(demo0719.index[0],demo0719.index[-1],freq='1min'))
纵坐标设置显示百分比
import matplotlib.ticker as mtick
fmt='%.2f%%'
yticks = mtick.FormatStrFormatter(fmt)
ax2.yaxis.set_major_formatter(yticks)
知识点
在matplotlib中,整个图像为一个Figure对象。在Figure对象中可以包含一个,或者多个Axes对象。每个Axes对象都是一个拥有自己坐标系统的绘图区域。其逻辑关系如下: