溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

matplotlib命令與格式之tick坐標(biāo)軸日期格式的示例分析

發(fā)布時(shí)間:2021-07-30 13:51:41 來(lái)源:億速云 閱讀:745 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了matplotlib命令與格式之tick坐標(biāo)軸日期格式的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

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()

matplotlib命令與格式之tick坐標(biāo)軸日期格式的示例分析

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()

matplotlib命令與格式之tick坐標(biāo)軸日期格式的示例分析

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“matplotlib命令與格式之tick坐標(biāo)軸日期格式的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI