當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python matplotlib AutoDateFormatter用法及代碼示例

本文簡要介紹 python 語言中 matplotlib.dates.AutoDateFormatter 的用法。

用法

class matplotlib.dates.AutoDateFormatter(locator, tz=None, defaultfmt='%Y-%m-%d', *, usetex=None)

基礎: Formatter

Formatter 嘗試找出最佳使用格式。與 AutoDateLocator 一起使用時最有用。

AutoDateFormatter 有一個 .scale 字典,用於映射刻度刻度(一個主要刻度之間的天數間隔)以格式化字符串;該字典默認為

self.scaled = {
    DAYS_PER_YEAR: rcParams['date.autoformatter.year'],
    DAYS_PER_MONTH: rcParams['date.autoformatter.month'],
    1: rcParams['date.autoformatter.day'],
    1 / HOURS_PER_DAY: rcParams['date.autoformatter.hour'],
    1 / MINUTES_PER_DAY: rcParams['date.autoformatter.minute'],
    1 / SEC_PER_DAY: rcParams['date.autoformatter.second'],
    1 / MUSECONDS_PER_DAY: rcParams['date.autoformatter.microsecond'],
}

格式化程序使用與字典中大於或等於當前比例的最低鍵對應的格式字符串。字典條目可以自定義:

locator = AutoDateLocator()
formatter = AutoDateFormatter(locator)
formatter.scaled[1/(24*60)] = '%M:%S' # only show min and sec

也可以使用自定義可調用對象來代替格式字符串。以下示例顯示如何使用自定義格式函數從小數秒中去除尾隨零並將日期添加到第一個刻度標簽:

def my_format_function(x, pos=None):
    x = matplotlib.dates.num2date(x)
    if pos == 0:
        fmt = '%D %H:%M:%S.%f'
    else:
        fmt = '%H:%M:%S.%f'
    label = x.strftime(fmt)
    label = label.rstrip("0")
    label = label.rstrip(".")
    return label

formatter.scaled[1/(24*60)] = my_format_function

自動格式化日期標簽。

參數
locator ticker.Locator

該軸正在使用的定位器。

tz str 或 tzinfo ,默認值:rcParams["timezone"](默認值:'UTC')

勾選時區。如果是字符串,則將 tz 傳遞給 dateutil.tz

defaultfmt str

如果 self.scaled 中的值都不大於 locator._get_unit() 返回的單位,則使用默認格式。

usetex 布爾值,默認值:rcParams["text.usetex"](默認值:False)

啟用/禁用使用 TeX 的數學模式來呈現格式化程序的結果。如果 self.scaled 中的任何條目被設置為函數,則由自定義函數來啟用或禁用 TeX 的數學模式本身。

相關用法


注:本文由純淨天空篩選整理自skytowner.com大神的英文原創作品 matplotlib.dates.AutoDateFormatter。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。