Matplotlib是Python中令人惊叹的可视化库,用于二维阵列图。 Matplotlib是一个基于NumPy数组构建的multi-platform数据可视化库,旨在与更广泛的SciPy堆栈配合使用。
matplotlib.dates.drange()
的matplotlib.dates.drange()
函数返回等距的Matplotlib日期序列。日期范围从“ dstart”开始,一直到,但不包括“ dend”。每个日期之间的间隔称为增量。
用法: matplotlib.dates.drange(dstart, dend, delta)
参数:
- dstart:日期范围的起点,是python的datetime日期。
- dend:日期范围的终点,是python的datetime日期。
- delta:它代表每个日期之间的间隔,属于python的datetime.timedelta。
返回值:它返回一个numpy数组,该数组是代表Matplotlib日期的浮点列表。
范例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)
输出:
相关用法
注:本文由纯净天空筛选整理自RajuKumar19大神的英文原创作品 Matplotlib.dates.drange() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。