欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

matplotlib命令與格式之tick坐標軸日期格式(設(shè)置日期主副刻度)

 更新時間:2019年08月06日 09:42:58   作者:開碼牛  
這篇文章主要介紹了matplotlib命令與格式之tick坐標軸日期格式(設(shè)置日期主副刻度),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1.橫坐標設(shè)置時間格式

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# 配置橫坐標為日期格式
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
# 生成橫縱坐標信息
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))
# 配置橫坐標
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() # 自動旋轉(zhuǎn)日期標記
plt.show()

2.設(shè)置日期坐標軸主副刻度值

所有坐標軸日期格式類型

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)獲取坐標軸日期格式類型

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個選1個)
rule = rrulewrapper(YEARLY, byeaster=1, interval=5)
loc = RRuleLocator(rule)

(2)設(shè)置坐標軸日期格式

#設(shè)置主副刻度
ax.xaxis.set_major_locator(mondays)ax.xaxis.set_minor_locator(alldays)
#設(shè)置坐標軸刻度標簽格式
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
 
#銷售數(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è)績趨勢走向')
plt.gcf().autofmt_xdate() # 自動旋轉(zhuǎn)日期標記
 
#設(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小時為1副刻度
ax1.xaxis.set_minor_locator(hoursLoc)
ax1.xaxis.set_minor_formatter(mdates.DateFormatter('%H'))
#參數(shù)pad用于設(shè)置刻度線與標簽間的距離
ax1.tick_params(pad=10)
 
#顯示圖像
plt.show()

3.設(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()


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • opencv實現(xiàn)文檔矯正

    opencv實現(xiàn)文檔矯正

    這篇文章主要為大家詳細介紹了opencv實現(xiàn)文檔矯正功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • pytorch實現(xiàn)textCNN的具體操作

    pytorch實現(xiàn)textCNN的具體操作

    這篇文章主要介紹了pytorch實現(xiàn)textCNN的具體操作流程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 對python 通過ssh訪問數(shù)據(jù)庫的實例詳解

    對python 通過ssh訪問數(shù)據(jù)庫的實例詳解

    今天小編就為大家分享一篇對python 通過ssh訪問數(shù)據(jù)庫的實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • python3中的md5加密實例

    python3中的md5加密實例

    今天小編就為大家分享一篇python3中的md5加密實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • python中 chr unichr ord函數(shù)的實例詳解

    python中 chr unichr ord函數(shù)的實例詳解

    這篇文章主要介紹了python中 chr unichr ord函數(shù)的實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-08-08
  • python 實現(xiàn)文件的遞歸拷貝實現(xiàn)代碼

    python 實現(xiàn)文件的遞歸拷貝實現(xiàn)代碼

    今天翻電腦時突然發(fā)現(xiàn)有個存了很多照片和視頻的文件夾,想起來是去年換手機(流行的小5)時拷出來的??戳藥讖堈掌掠忠荒荒坏母‖F(xiàn)在腦海,好吧,我是個感性的人
    2012-08-08
  • 關(guān)于Pyinstaller打包eel和pygame需要注意的坑

    關(guān)于Pyinstaller打包eel和pygame需要注意的坑

    這篇文章主要介紹了關(guān)于Pyinstaller打包eel和pygame需要注意的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • python實現(xiàn)進度條的多種實現(xiàn)

    python實現(xiàn)進度條的多種實現(xiàn)

    這篇文章主要介紹了python實現(xiàn)進度條的多種實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • DJANGO-URL反向解析REVERSE實例講解

    DJANGO-URL反向解析REVERSE實例講解

    在本篇文章里小編給大家整理的是一篇關(guān)于DJANGO-URL反向解析REVERSE的相關(guān)知識點內(nèi)容,需要的朋友們學習下。
    2019-10-10
  • Python必備技巧之字符數(shù)據(jù)操作詳解

    Python必備技巧之字符數(shù)據(jù)操作詳解

    處理字符數(shù)據(jù)是編程不可或缺的一部分。Python?提供了一組豐富的運算符、函數(shù)和方法來處理字符串。包括字符串運算符、內(nèi)置函數(shù)、索引、切片和內(nèi)置方法??靵韺W習一下吧
    2022-03-03

最新評論