Matplotlib,绘制 pandas 系列:AttributeError: 'tuple' 对象没有属性 'xaxis' [英] Matplotlib, plotting pandas series: AttributeError: 'tuple' object has no attribute 'xaxis'

查看:59
本文介绍了Matplotlib,绘制 pandas 系列:AttributeError: 'tuple' 对象没有属性 'xaxis'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制 Pandas 系列数据,该数据记录了 1981 年每周事件"的总和.该系列名为weekly_data".

I am plotting Pandas Series data, which records the sum of "events" each week in 1981. The series is named '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:

I would like to place ticks by year, and by week. When I try to plot this, I receive an 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'

我该如何解决这个问题?

How can I fix this?

在 Mike Mueller 之后,上面的错误是 plt.subplot(111).但是,我仍然无法获得每周的工作时间.也许我们需要使用 ax.set_xticks(major_ticks)ax.set_xticks(minor_ticks, minor=True)

Following Mike Mueller, the error above was plt.subplot(111). However, I still cannot get weekly ticks to work. Perhaps we need to use ax.set_xticks(major_ticks) or ax.set_xticks(minor_ticks, minor=True)

这是我从 1991 年开始绘制的熊猫系列数据

Here is the pandas series data I am plotting from 1991

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()

这是绘图输出

我自己很困惑

推荐答案

您正在创建 111 个子图.更改:

You are creating 111 subplots. Change:

ax = plt.subplots(111)

进入:

ax = plt.subplot(111)

你应该可以工作.它创建一个包含一行、一列和一个子图的子图数组.例如,这个:

and you it should work. It creates one subplot array of one row, one column, and one subplot. For example, this:

plt.subplot(231)
plt.subplot(236)

在 2 行 3 列的数组中创建子图 1 和子图 6:

creates subplot 1 and subplot 6 in array of 2 rows and 3 columns:

您可以通过以下方式显示月份:

You make the months visible with:

ax.xaxis.set_minor_formatter(matplotlib.dates.DateFormatter('%B'))

年和月印在彼此之上.一种解决方案是将年份放在顶部,月份放在底部:

Th year an the month are printed on top of each other. One solution is to have the years on the top and the months on bottom:

ax.xaxis.set_tick_params(labeltop='on', labelbottom='off')

使用:

mloc = matplotlib.dates.MonthLocator(range(1, 12, 4))

仅每四个月显示一次.

这篇关于Matplotlib,绘制 pandas 系列:AttributeError: 'tuple' 对象没有属性 'xaxis'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆