Python之matplotlib学习(三)

时间:2021-02-19 00:14:28

例子11-1:横坐标时间的处理

from matplotlib.dates import datestr2num,DateFormatter

import matplotlib.dates as dates

fig,ax=plt.subplots()
formatter = DateFormatter('%H')
#设置时间间隔
ax.xaxis.set_major_locator(dates.HourLocator(byhour=range(24), interval=4))
ax.xaxis.set_major_formatter(formatter)
ax.plot_date(datestr2num(time_list),iops_list,'-',label='iops')

 

输出结果:

Python之matplotlib学习(三)

例子11-2:纵坐标处理

原图:

Python之matplotlib学习(三)

 

从图上看纵坐标区间过大,不利用查看。

改成200为步长:

import matplotlib.ticker as tk

ax.yaxis.set_major_locator(tk.MultipleLocator(200)) 

 

输出结果:

Python之matplotlib学习(三)

例子11-3:纵坐标处理——按照分钟进行间隔

如下是原始输出的图

Python之matplotlib学习(三)

时间轴每5分钟一次,过于太密,想改成如下效果:

Python之matplotlib学习(三)

按照分钟级别进行汇总,代码修改的地方如下:

import matplotlib.dates as dates
ax.xaxis.set_major_locator(dates.MinuteLocator(byminute=range(60), interval=10))

>>> import matplotlib.dates as dates
>>> help(dates)

#查看帮助文档
Here are all the date tickers:
    
        * :class:`MinuteLocator`: locate minutes
    
        * :class:`HourLocator`: locate hours
    
        * :class:`DayLocator`: locate specifed days of the month
    
        * :class:`WeekdayLocator`: Locate days of the week, eg MO, TU
    
        * :class:`MonthLocator`: locate months, eg 7 for july
    
        * :class:`YearLocator`: locate years that are multiples of base
    

     |  ----------------------------------------------------------------------
     |  Data and other attributes inherited from DateLocator:
     |  
     |  hms0d = {'byhour': 0, 'byminute': 0, 'bysecond': 0}