matplotlib命令與格式之tick坐標(biāo)軸日期格式(設(shè)置日期主副刻度)
1.橫坐標(biāo)設(shè)置時(shí)間格式
import matplotlib.pyplot as plt import matplotlib.dates as mdates # 配置橫坐標(biāo)為日期格式 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d')) plt.gca().xaxis.set_major_locator(mdates.DayLocator())
例子:
from datetime import datetime import matplotlib.dates as mdates import matplotlib.pyplot as plt # 生成橫縱坐標(biāo)信息 dates = ['01/02/1991', '01/03/1991', '01/04/1991'] xs = [datetime.strptime(d, '%m/%d/%Y').date() for d in dates] ys = range(len(xs)) # 配置橫坐標(biāo) plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y')) plt.gca().xaxis.set_major_locator(mdates.DayLocator()) # Plot plt.plot(xs, ys) plt.gcf().autofmt_xdate() # 自動(dòng)旋轉(zhuǎn)日期標(biāo)記 plt.show()
2.設(shè)置日期坐標(biāo)軸主副刻度值
所有坐標(biāo)軸日期格式類(lèi)型
MinuteLocator: locate minutes(f)
HourLocator: locate hours
DayLocator: locate specified days of the month
WeekdayLocator: Locate days of the week, e.g., MO, TU
MonthLocator: locate months, e.g., 7 for july
YearLocator: locate years that are multiples of base
RRuleLocator: locate using a matplotlib.dates.rrulewrapper. The rrulewrapper is a simple wrapper around adateutil.rrule (dateutil) which allow almost arbitrary date tick specifications. See rrule example.
AutoDateLocator: On autoscale, this class picks the best MultipleDateLocator to set the view limits and the tick locations.
(1)獲取坐標(biāo)軸日期格式類(lèi)型
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY #獲取每月一日數(shù)據(jù) monthdays = MonthLocator() #獲取每周一的日期數(shù)據(jù) mondays = WeekdayLocator(MONDAY) #獲取每日數(shù)據(jù) alldays = DayLocator() # import constants for the days of the week from matplotlib.dates import MO, TU, WE, TH, FR, SA, SU # tick on mondays every week loc = WeekdayLocator(byweekday=MO, tz=tz) # tick on mondays and saturdays loc = WeekdayLocator(byweekday=(MO, SA)) # tick on mondays every second week loc = WeekdayLocator(byweekday=MO, interval=2) # tick every 5th easter(每隔5個(gè)選1個(gè)) rule = rrulewrapper(YEARLY, byeaster=1, interval=5) loc = RRuleLocator(rule)
(2)設(shè)置坐標(biāo)軸日期格式
#設(shè)置主副刻度 ax.xaxis.set_major_locator(mondays)ax.xaxis.set_minor_locator(alldays) #設(shè)置坐標(biāo)軸刻度標(biāo)簽格式 mondayFormatter = DateFormatter('%Y-%m-%d') # 如:2-29-2015dayFormatter = DateFormatter('%d') # 如:12ax.xaxis.set_major_formatter(mondayFormatter) #字符串旋轉(zhuǎn) for label in ax1.get_xticklabels(): label.set_rotation(30) label.set_horizontalalignment('right')
(3)例子
import matplotlib.pyplot as plt import matplotlib.dates as mdates from datetime import datetime #銷(xiāo)售數(shù)據(jù) dates=[20171101,20171102,20171103,20171104] sales=[102.1,100.6,849,682] #將dates改成日期格式 x= [datetime.strptime(str(d), '%Y%m%d').date() for d in dates] #figure布局 fig=plt.figure(figsize=(8,4)) ax1=fig.add_subplot(1,1,1) #繪圖 ax1.plot(x,y,ls='--',lw=3,color='b',marker='o',ms=6, mec='r',mew=2, mfc='w',label='業(yè)績(jī)趨勢(shì)走向') plt.gcf().autofmt_xdate() # 自動(dòng)旋轉(zhuǎn)日期標(biāo)記 #設(shè)置x軸主刻度格式 alldays = mdates.DayLocator() #主刻度為每天 ax1.xaxis.set_major_locator(alldays) #設(shè)置主刻度 ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y%m%d')) #設(shè)置副刻度格式 hoursLoc = mpl.dates.HourLocator(interval=6) #為6小時(shí)為1副刻度 ax1.xaxis.set_minor_locator(hoursLoc) ax1.xaxis.set_minor_formatter(mdates.DateFormatter('%H')) #參數(shù)pad用于設(shè)置刻度線與標(biāo)簽間的距離 ax1.tick_params(pad=10) #顯示圖像 plt.show()
3.設(shè)置日期時(shí)間刻度值
import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl import datetime as dt fig = plt.figure() ax2 = fig.add_subplot(212) date2_1 = dt.datetime(2008,9,23) date2_2 = dt.datetime(2008,10,3) delta2 = dt.timedelta(days=1) dates2 = mpl.dates.drange(date2_1, date2_2, delta2) y2 = np.random.rand(len(dates2)) ax2.plot_date(dates2, y2, linestyle='-') dateFmt = mpl.dates.DateFormatter('%Y-%m-%d') ax2.xaxis.set_major_formatter(dateFmt) daysLoc = mpl.dates.DayLocator() hoursLoc = mpl.dates.HourLocator(interval=6) ax2.xaxis.set_major_locator(daysLoc) ax2.xaxis.set_minor_locator(hoursLoc) fig.autofmt_xdate(bottom=0.18) fig.subplots_adjust(left=0.18) ax1 = fig.add_subplot(211) date1_1 = dt.datetime(2008, 9, 23) date1_2 = dt.datetime(2009, 2, 16) delta1 = dt.timedelta(days=10) dates1 = mpl.dates.drange(date1_1, date1_2, delta1) y1 = np.random.rand(len(dates1)) ax1.plot_date(dates1, y1, linestyle='--') monthsLoc = mpl.dates.MonthLocator() weeksLoc = mpl.dates.WeekdayLocator() ax1.xaxis.set_major_locator(monthsLoc) ax1.xaxis.set_minor_locator(weeksLoc) monthsFmt = mpl.dates.DateFormatter('%b') ax1.xaxis.set_major_formatter(monthsFmt) plt.show()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch實(shí)現(xiàn)textCNN的具體操作
這篇文章主要介紹了pytorch實(shí)現(xiàn)textCNN的具體操作流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05對(duì)python 通過(guò)ssh訪問(wèn)數(shù)據(jù)庫(kù)的實(shí)例詳解
今天小編就為大家分享一篇對(duì)python 通過(guò)ssh訪問(wèn)數(shù)據(jù)庫(kù)的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02python中 chr unichr ord函數(shù)的實(shí)例詳解
這篇文章主要介紹了python中 chr unichr ord函數(shù)的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-08-08python 實(shí)現(xiàn)文件的遞歸拷貝實(shí)現(xiàn)代碼
今天翻電腦時(shí)突然發(fā)現(xiàn)有個(gè)存了很多照片和視頻的文件夾,想起來(lái)是去年換手機(jī)(流行的小5)時(shí)拷出來(lái)的??戳藥讖堈掌?,往事又一幕幕的浮現(xiàn)在腦海,好吧,我是個(gè)感性的人2012-08-08關(guān)于Pyinstaller打包eel和pygame需要注意的坑
這篇文章主要介紹了關(guān)于Pyinstaller打包eel和pygame需要注意的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02python實(shí)現(xiàn)進(jìn)度條的多種實(shí)現(xiàn)
這篇文章主要介紹了python實(shí)現(xiàn)進(jìn)度條的多種實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04DJANGO-URL反向解析REVERSE實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于DJANGO-URL反向解析REVERSE的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-10-10