【发布时间】:2019-08-16 17:42:25
【问题描述】:
我在 jupyter 笔记本中使用 matplotlib 绘制了一个时间序列图。
import numpy as np
import matplotlib.pyplot as plt
from numpy.polynomial import polynomial as P
from datetime import date
首先,我创建初始图并描述 x 和 y:
fig, ax = plt.subplots(1,1,dpi=200)
dates = ['2017-08', '2017-09', '2017-10', '2017-11', '2017-12', '2018-01',
'2018-02', '2018-03', '2018-04', '2018-05', '2018-06', '2018-07',
'2018-08', '2018-09', '2018-10', '2018-11', '2018-12', '2019-01',
'2019-02', '2019-03', '2019-04', '2019-05', '2019-06', '2019-07']
y = [2002, 2630, 2032, 1816, 1867, 2282, 2064, 2316, 2391, 2134, 1833, 1982, 2053, 1836, 2107, 1891, 1729, 1794, 1908, 2267, 2194, 2248, 2216, 2408,]
# Convert to ordinal dates here so that computation of x degree polynomial is easier
x = list(map(lambda x: date.fromisoformat(f'{x}-01').toordinal(), dates))
其次,我在图中添加两条线(线和多边形拟合):
# plot the line
line, = ax.plot(x, y, 'k', linewidth=3, label='Awesome SO answers')
# create 2nd degree Polynomial series instance
c1 = P.Polynomial.fit(x, y, 2)
# Returns the x, y values at n linearly spaced points across the domain.
x1, y1 = c1.linspace(n=len(x))
poly2, = ax.plot(x1, y1, 'r', linewidth=3, label='2nd deg poly fit')
# create labels and call set_xticklabels
# I think this is were the problem is.
# How do I set the x ticks to include the first/last date?
new_labels = [date.fromordinal(int(xt)) for xt in ax.get_xticks()]
ax.set_xticklabels(new_labels)
# make plot look nicer, add legend
ax.xaxis.set_tick_params(rotation=30, labelsize=8)
first_legend = ax.legend(handles=[line, poly2], loc='best')
看到下面的输出似乎隐藏了第一个/最后一个日期。请注意,输出中 x 上的最后一个数据点是“2019-05”,但实际上是“2019-07”。 最好包含最后日期,以便清楚数据是最新的。 matplotlib 必须使用一些规则来计算 xticks。如何让 xticks 尊重第一个/最后一个日期?
【问题讨论】:
-
哦,我想我明白了。首先使用
np.linspace(min(x), max(x), num=6)在指定间隔内创建均匀间隔的数组,然后使用输出调用ax.set_xticks()。
标签: python-3.x numpy matplotlib plot