成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Matplotlib繪圖雙縱坐標軸設置及控制設置時間格式

開發 開發工具
今天利用matplotlib繪圖,想要完成一個雙坐標格式的圖。

 雙y軸坐標軸圖

今天利用matplotlib繪圖,想要完成一個雙坐標格式的圖。 

  1. fig=plt.figure(figsize=(20,15)) 
  2. ax1=fig.add_subplot(111) 
  3. ax1.plot(demo0719['TPS'],'b-',label='TPS',linewidth=2) 
  4. ax2=ax1.twinx()#這是雙坐標關鍵一步 
  5. ax2.plot(demo0719['successRate']*100,'r-',label='successRate',linewidth=2)  

橫坐標設置時間間隔 

  1. import matplotlib.dates as mdate 
  2. ax1.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d %H:%M:%S'))#設置時間標簽顯示格式 
  3. plt.xticks(pd.date_range(demo0719.index[0],demo0719.index[-1],freq='1min'))  

縱坐標設置顯示百分比 

  1. import matplotlib.ticker as mtick 
  2.  
  3. fmt='%.2f%%' 
  4.  
  5. yticks = mtick.FormatStrFormatter(fmt) 
  6.  
  7. 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()

  • 官網matplotlib.pyplot.figure
    pyplot.figure()是返回一個Figure對象的,也就是一張圖片。
  • add_subplot(args, *kwargs)

The Axes instance will be returned.

twinx()

  • matplotlib.axes.Axes method2 
  1. 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_major_formatter(formatter)

Set the formatter of the major ticker

ACCEPTS: A Formatter instance

DateFormatter()

  • class matplotlib.dates.DateFormatter(fmt, tz=None)
    這是一個類,創建一個時間格式的實例。

strftime方法(傳入格式化字符串)。

 

  1. strftime(dt, fmt=None) 
  2.  
  3. Refer to documentation for datetime.strftime. 
  4.  
  5. fmt is a strftime() format string. 

 

FormatStrFormatter()

  • class matplotlib.ticker.FormatStrFormatter(fmt)

Use a new-style format string (as used by str.format()) to format the tick. The field formatting must be labeled x

定義字符串格式。

plt.xticks

  • matplotlib.pyplot.xticks(args, *kwargs)
  1. return locs, labels where locs is an array of tick locations and 
  2. # labels is an array of tick labels. 
  3. locs, labels = xticks() 
  4.  
  5. set the locations of the xticks 
  6. xticks( arange(6) ) 
  7.  
  8. set the locations and labels of the xticks 
  9. xticks( arange(5), ('Tom''Dick''Harry''Sally''Sue') ) 

 

代碼匯總

  1. #coding:utf-8 
  2.  
  3. import matplotlib.pyplot as plt 
  4.  
  5. import matplotlib as mpl 
  6.  
  7. import matplotlib.dates as mdate 
  8.  
  9. import matplotlib.ticker as mtick 
  10.  
  11. import numpy as np 
  12.  
  13. import pandas as pd 
  14.  
  15. import os 
  16.  
  17. mpl.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標簽 
  18.  
  19. mpl.rcParams['axes.unicode_minus']=False #用來正常顯示負號 
  20.  
  21. mpl.rc('xtick', labelsize=20) #設置坐標軸刻度顯示大小 
  22.  
  23. mpl.rc('ytick', labelsize=20) 
  24.  
  25. font_size=30 
  26.  
  27. #matplotlib.rcParams.update({'font.size': 60}) 
  28.  
  29. %matplotlib inline 
  30.  
  31. plt.style.use('ggplot'
  32.  
  33. data=pd.read_csv('simsendLogConvert_20160803094801.csv',index_col=0,encoding='gb2312',parse_dates=True
  34.  
  35. columns_len=len(data.columns) 
  36.  
  37. data_columns=data.columns 
  38.  
  39. for x in range(0,columns_len,2): 
  40.  
  41. print('第{}列'.format(x)) 
  42.  
  43. total=data.ix[:,x] 
  44.  
  45. print('第{}列'.format(x+1)) 
  46.  
  47. successRate=(data.ix[:,x+1]/data.ix[:,x]).fillna(0) 
  48.  
  49. yLeftLabel=data_columns[x] 
  50.  
  51. yRightLable=data_columns[x+1] 
  52.  
  53. print('------------------開始繪制類型{}曲線圖------------------'.format(data_columns[x])) 
  54.  
  55. fig=plt.figure(figsize=(25,20)) 
  56.  
  57. ax1=fig.add_subplot(111) 
  58.  
  59. #繪制Total曲線圖 
  60.  
  61. ax1.plot(total,color='#4A7EBB',label=yLeftLabel,linewidth=4) 
  62.  
  63. # 設置X軸的坐標刻度線顯示間隔 
  64.  
  65. ax1.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d %H:%M:%S'))#設置時間標簽顯示格式 
  66.  
  67. plt.xticks(pd.date_range(data.index[0],data.index[-1],freq='1min'))#時間間隔 
  68.  
  69. plt.xticks(rotation=90) 
  70.  
  71. #設置雙坐標軸,右側Y軸 
  72.  
  73. ax2=ax1.twinx() 
  74.  
  75. #設置右側Y軸顯示百分數 
  76.  
  77. fmt='%.2f%%' 
  78.  
  79. yticks = mtick.FormatStrFormatter(fmt) 
  80.  
  81. # 繪制成功率圖像 
  82.  
  83. ax2.set_ylim(0,110) 
  84.  
  85. ax2.plot(successRate*100,color='#BE4B48',label=yRightLable,linewidth=4) 
  86.  
  87. ax2.yaxis.set_major_formatter(yticks) 
  88.  
  89. ax1.set_xlabel('Time',fontsize=font_size) 
  90.  
  91. ax1.set_ylabel(yLeftLabel,fontsize=font_size) 
  92.  
  93. ax2.set_ylabel(yRightLable,fontsize=font_size) 
  94.  
  95. legend1=ax1.legend(loc=(.02,.94),fontsize=16,shadow=True
  96.  
  97. legend2=ax2.legend(loc=(.02,.9),fontsize=16,shadow=True
  98.  
  99. legend1.get_frame().set_facecolor('#FFFFFF'
  100.  
  101. legend2.get_frame().set_facecolor('#FFFFFF'
  102.  
  103. plt.title(yLeftLabel+'&'+yRightLable,fontsize=font_size) 
  104.  
  105. plt.savefig('D:\\JGT\\Work-YL\\01布置的任務\\04繪制曲線圖和報告文件\\0803\\出圖\\{}-{}'.format(yLeftLabel.replace(r'/',' '),yRightLable.replace(r'/',' ')),dpi=300)  

參考

 

  1. Vami-繪圖: matplotlib核心剖析 
  2. Secondary axis with twinx(): how to add to legend? 

 

 

責任編輯:龐桂玉 來源: segmentfault
相關推薦

2015-07-29 10:25:05

數據開發產品必修課

2013-07-29 04:57:47

iOS開發iOS開發學習時間設置和格式輸出

2024-12-13 09:42:38

javascripmatch方法

2009-12-22 09:04:35

ACL時間控制列表

2022-07-25 14:17:04

JS應用開發

2023-04-07 08:02:30

圖形編輯器對齊功能

2017-04-06 15:49:18

機器人控制方式特點

2009-12-24 17:41:12

ADO Connect

2011-01-19 14:32:17

Thunderbird設置

2012-02-02 10:23:41

2013-04-01 14:40:34

App內購家長蘋果

2010-07-26 09:57:21

telnet pop

2010-09-29 16:51:10

訪問控制

2013-05-30 09:58:50

RouterosADSL負載均衡技術

2019-03-24 19:16:35

FedoraSSH系統運維

2009-12-03 15:24:39

雙wan路由器設置

2011-07-27 14:48:21

iPhone Cocos2D 坐標

2012-10-11 15:28:18

設置默認打印機
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲精品视频三区 | 日韩中文字幕免费 | 久久久久久国产精品 | 中文字幕不卡在线88 | 亚洲免费一 | 国产精品久久网 | 久久综合一区二区三区 | 久久精品免费一区二区三 | 狠狠干2020 | 在线欧美 | 谁有毛片 | 欧美在线视频一区二区 | 亚洲精品中文字幕中文字幕 | 能看的av| 美女一级毛片 | 国产精品美女久久久久久免费 | 国产精品99久久久久久宅男 | 狠狠操天天操 | 欧美综合久久久 | 国产极品粉嫩美女呻吟在线看人 | 欧美视频在线播放 | 久久精品欧美一区二区三区不卡 | 在线观看成人小视频 | 日韩av在线免费 | 欧美在线一区二区三区 | 久久极品| 中文字幕在线观看视频一区 | 嫩草网| 天天夜碰日日摸日日澡 | 91新视频 | 91精品国产综合久久精品 | 毛片免费观看视频 | 亚洲一区二区在线视频 | 午夜三级视频 | 成人影院在线视频 | 亚洲国产一区视频 | 成人亚洲视频 | 色综合天天网 | 亚洲激情专区 | av片在线播放 | 欧美在线天堂 |