📌  相关文章
📜  Python中的 Matplotlib.figure.Figure.autofmt_xdate()

📅  最后修改于: 2022-05-13 01:54:31.247000             🧑  作者: Mango

Python中的 Matplotlib.figure.Figure.autofmt_xdate()

Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 figure 模块提供了顶级 Artist,即 Figure,其中包含所有绘图元素。该模块用于控制所有绘图元素的子图和顶级容器的默认间距。

matplotlib.figure.Figure.autofmt_xdate()函数

matplotlib 库的 autofmt_xdate() 方法图形模块用于旋转它们并右对齐它们。

下面的示例说明了 matplotlib.figure 中的 matplotlib.figure.Figure.autofmt_xdate()函数:

示例 1:

# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.cbook as cbook
    
years = mdates.YearLocator()   
months = mdates.MonthLocator()  
years_fmt = mdates.DateFormatter('% Y')
    
with cbook.get_sample_data('goog.npz') as datafile:
    data = np.load(datafile)['price_data'].view(np.recarray)
        
fig, ax = plt.subplots()
ax.plot('date', 'adj_close', 
         data = data[:300],
         color ="green")
    
ax.xaxis.set_major_locator(years) 
ax.format_ydata = lambda x: '$% 1.2f' % x
ax.grid(True)
   
fig.autofmt_xdate()
   
fig.suptitle('matplotlib.figure.Figure.autofmt_xdate() \
function Example\n\n', fontweight ="bold")
  
plt.show()

输出:

示例 2:

# Implementation of matplotlib function
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
import numpy as np
    
date1 = datetime.datetime(2020, 4, 2)
date2 = datetime.datetime(2020, 4, 6)
delta = datetime.timedelta(hours = 6)
dates = drange(date1, date2, delta)
    
y = np.arange(len(dates))
    
fig, ax = plt.subplots()
ax.plot_date(dates, y ** 2, 'g')
    
ax.set_xlim(dates[0], dates[-1])
    
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_minor_locator(HourLocator(range(0, 25, 6)))
ax.xaxis.set_major_formatter(DateFormatter('% Y-% m-% d'))
    
ax.fmt_xdata = DateFormatter('% Y-% m-% d % H:% M:% S')
fig.autofmt_xdate()
   
fig.suptitle('matplotlib.figure.Figure.autofmt_xdate() \
function Example\n\n', fontweight ="bold")
  
plt.show()

输出: