python绘图设置时间坐标轴_Matplotlib绘图双纵坐标轴设置及
控制设置时间格式
双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
matplotlib中subplotax1.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d %H:%M:%S'))#设置时间标签显⽰格式
纵坐标设置显⽰百分⽐
import matplotlib.ticker as mtick
fmt='%.2f%%'
yticks = mtick.FormatStrFormatter(fmt)
ax2.yaxis.set_major_formatter(yticks)
知识点
在matplotlib中,整个图像为⼀个Figure对象。在Figure对象中可以包含⼀个,或者多个Axes对象。每个Axes对象都是⼀个拥有⾃⼰坐标系统的绘图区域。其逻辑关系如下:
⼀个Figure对应⼀张图⽚。
Title为标题。Axis为坐标轴,Label为坐标轴标注。Tick为刻度线,Tick Label为刻度注释。
Title为标题。Axis为坐标轴,Label为坐标轴标注。Tick为刻度线,Tick Label为刻度注释。
add_subplot()
The Axes instance will be returned.
twinx()
ax = twinx()
create a twin of Axes for generating a plot with a sharex x-axis but independent y axis. The y-axis of self will have ticks on left and the returned axes will have ticks on the right.
意思就是,创建了⼀个独⽴的Y轴,共享了X轴。双坐标轴!
类似的还有twiny()
ax1.xaxis.set_major_formatter
Set the formatter of the major ticker
ACCEPTS: A Formatter instance
DateFormatter()
strftime⽅法(传⼊格式化字符串)。
strftime(dt, fmt=None)
Refer to documentation for datetime.strftime.
fmt is a strftime() format string.
FormatStrFormatter()
Use a new-style format string (as used by str.format()) to format the tick. The field formatting must be labeled x 定义字符串格式。
# return locs, labels where locs is an array of tick locations and
# labels is an array of tick labels.
locs, labels = xticks()
# set the locations of the xticks
xticks( arange(6) )
# set the locations and labels of the xticks
xticks( arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue') )
代码汇总
#coding:utf-8
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.dates as mdate
import matplotlib.ticker as mtick
import numpy as np
import pandas as pd
import os
<('xtick', labelsize=20) #设置坐标轴刻度显⽰⼤⼩
<('ytick', labelsize=20)
font_size=30
#Params.update({'font.size': 60})
%matplotlib inline
plt.style.use('ggplot')
ad_csv('simsendLogConvert_20160803094801.csv',index_col=0,encoding='gb2312',parse_dates=True) columns_len=lumns)
data_lumns
for x in range(0,columns_len,2):
print('第{}列'.format(x))
total=data.ix[:,x]
print('第{}列'.format(x+1))
successRate=(data.ix[:,x+1]/data.ix[:,x]).fillna(0)
yLeftLabel=data_columns[x]
yRightLable=data_columns[x+1]
print('------------------开始绘制类型{}曲线图------------------'.format(data_columns[x]))
fig=plt.figure(figsize=(25,20))
ax1=fig.add_subplot(111)
#绘制Total曲线图
ax1.plot(total,color='#4A7EBB',label=yLeftLabel,linewidth=4)
# 设置X轴的坐标刻度线显⽰间隔
ax1.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d %H:%M:%S'))#设置时间标签显⽰格式
#设置双坐标轴,右侧Y轴
ax2=ax1.twinx()
#设置右侧Y轴显⽰百分数
fmt='%.2f%%'
yticks = mtick.FormatStrFormatter(fmt)
# 绘制成功率图像
ax2.set_ylim(0,110)
ax2.plot(successRate*100,color='#BE4B48',label=yRightLable,linewidth=4)
ax2.yaxis.set_major_formatter(yticks)
ax1.set_xlabel('Time',fontsize=font_size)
ax1.set_ylabel(yLeftLabel,fontsize=font_size)
ax2.set_ylabel(yRightLable,fontsize=font_size)
legend1=ax1.legend(loc=(.02,.94),fontsize=16,shadow=True)
legend2=ax2.legend(loc=(.02,.9),fontsize=16,shadow=True)
<_frame().set_facecolor('#FFFFFF')
<_frame().set_facecolor('#FFFFFF')
plt.title(yLeftLabel+'&'+yRightLable,fontsize=font_size)
plt.savefig('D:\\JGT\\Work-YL\\01布置的任务\\04绘制曲线图和报告⽂件\\0803\\出图\\{}-{}'.place(r'/',' '),place(r'/',' ')),dpi=300)
参考

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。