简体   繁体   English

绘制pandas中的子图时的IndexError

[英]IndexError when plotting subplots in pandas

I have a dataframe that looks like: 我有一个看起来像这样的数据框:

(Using ipython notebook..) (使用ipython笔记本..)

import pandas as pd
pd.options.display.mpl_style = 'default'
%matplotlib
import matplotlib.pyplot as plt

df = pd.DataFrame({'Average': {'April- 2014': 94.400000000000006,
  'August- 2014': 94.400000000000006,
  'December- 2014': 94.400000000000006,
  'February- 2015': 94.400000000000006,
  'January- 2015': 94.400000000000006,
  'July- 2014': 94.400000000000006,
  'June- 2014': 94.400000000000006,
  'May- 2014': 94.400000000000006,
  'November- 2014': 94.400000000000006,
  'October- 2014': 94.400000000000006,
  'September- 2014': 94.400000000000006},
 'Number': {'April- 2014': 80,
  'August- 2014': 86,
  'December- 2014': 110,
  'February- 2015': 11,
  'January- 2015': 104,
  'July- 2014': 90,
  'June- 2014': 83,
  'May- 2014': 108,
  'November- 2014': 118,
  'October- 2014': 127,
  'September- 2014': 107}})

Per the documentation listed here you should be able to do this: 根据此处列出的文档您应该能够这样做:

fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(15, 8))
df['Number'].plot(ax=axes[0, 0])

However, it results in: IndexError: too many indices for array 但是,它会导致: IndexError: too many indices for array

What's the easiest way to plot subplots? 绘制子图的最简单方法是什么?

I will copy Tom's answer from the comments here. 我将从这里的评论中复制汤姆的回答。

Check your axes.shape . 检查你的axes.shape It's (2,) so you only need .plot(ax=axes[0]) . 它是(2,)所以你只需要.plot(ax=axes[0]) There's also the subplots=True argument to DataFrame.plot DataFrame.plot subplots=True参数

It should work. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM