📜  Python中的 Matplotlib.dates.drange()

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

Python中的 Matplotlib.dates.drange()

Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。

matplotlib.dates.drange()

matplotlib.dates.drange()函数返回一系列等间距的 Matplotlib 日期。日期范围从 'dstart' 开始,直到,但不包括 'dend'。每个日期之间的间隔称为增量。

示例 1:

import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
import numpy as np
  
  
date_1 = datetime.datetime( 2020, 3, 2)
date_2 = datetime.datetime( 2020, 10, 10)
  
time_delta = datetime.timedelta(days = 28)
dates = drange(date_1, date_2, time_delta)
  
y_axis = np.arange( len(dates) )
  
fig, ax = plt.subplots()
ax.plot_date(dates, y_axis * y_axis)
  
ax.xaxis.set_major_formatter( DateFormatter('% Y-% m') )
  
plt.show()

输出:

示例 2:

import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
  
date = [datetime.datetime(2020, 8, 24, 0, 0),
        datetime.datetime(2020, 8, 23, 0, 0), 
        datetime.datetime(2020, 8, 22, 0, 0), 
        datetime.datetime(2020, 8, 21, 0, 0), 
        datetime.datetime(2020, 8, 18, 0, 0),
        datetime.datetime(2020, 8, 17, 0, 0),
        datetime.datetime(2020, 8, 16, 0, 0),
        datetime.datetime(2020, 8, 15, 0, 0),
        datetime.datetime(2020, 8, 14, 0, 0),
        datetime.datetime(2020, 8, 11, 0, 0),
        datetime.datetime(2020, 8, 10, 0, 0), 
        datetime.datetime(2020, 8, 9, 0, 0),
        datetime.datetime(2020, 8, 8, 0, 0),
        datetime.datetime(2020, 8, 7, 0, 0),
        datetime.datetime(2020, 8, 4, 0, 0),
        datetime.datetime(2020, 8, 3, 0, 0),
        datetime.datetime(2020, 8, 2, 0, 0),
        datetime.datetime(2020, 8, 1, 0, 0)]
  
# is a datetime.datetime object 
# according to type
start_date = date[0]
  
# is a datetime.datetime object according 
# to type    
end_date = date[-1]
delta = datetime.timedelta(days = 5)
  
# the drange function
dates = mdates.drange(start_date, end_date, -delta)
y_data = range(len(dates))
  
plt.plot(dates, y_data)

输出: