我正在绘制 Pandas 系列数据,它记录了 1981 年每周“事件”的总和。该系列被命名为“weekly_data”。
1981-03-16 1826
1981-03-23 1895
1981-03-30 1964
1981-04-06 1978
1981-04-13 2034
1981-04-20 2073
1981-04-27 2057
dtype: int64
我想按年和按周放置刻度。当我尝试绘制此图时,我收到一个 AttributeError:
fig = plt.figure(figsize=(12,5))
ax = plt.subplots(111)
plt.plot(weekly_data, color = 'green' )
yloc = YearLocator()
mloc = MonthLocator()
ax.xaxis.set_major_locator(yloc)
ax.xaxis.set_minor_locator(mloc)
ax.grid(True)
plt.show()
错误是
AttributeError Traceback (most recent call last)
<ipython-input-92-843dbab30ed7> in <module>()
6 yloc = YearLocator()
7 mloc = MonthLocator()
----> 8 ax.xaxis.set_major_locator(yloc)
9 ax.xaxis.set_minor_locator(mloc)
10 ax.grid(True)
AttributeError: 'tuple' object has no attribute 'xaxis'
我怎样才能解决这个问题?
编辑:继迈克·穆勒之后,上述错误是plt.subplot(111)
。但是,我still无法让每周的蜱虫发挥作用。也许我们需要使用ax.set_xticks(major_ticks)
or ax.set_xticks(minor_ticks, minor=True)
这是我从 1991 年开始绘制的 pandas 系列数据
Datetime
1990-12-23 1980
1990-12-30 1860
1991-01-06 1761
1991-01-13 1792
1991-01-20 1825
....
dtype: int64
这是代码
fig = plt.figure(figsize=(12,5))
ax = plt.subplot(111)
plt.plot(weekly_data1991, color = 'green' )
yloc = YearLocator()
mloc = MonthLocator()
ax.xaxis.set_major_locator(yloc)
ax.xaxis.set_minor_locator(mloc)
ax.grid(True)
plt.show()
这是绘图输出
我自己也很困惑