Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexError when plotting subplots in pandas

Tags:

python

pandas

I have a dataframe that looks like:

(Using ipython notebook..)

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

What's the easiest way to plot subplots?

like image 604
DataSwede Avatar asked Dec 01 '25 02:12

DataSwede


1 Answers

I will copy Tom's answer from the comments here.

Check your axes.shape. It's (2,) so you only need .plot(ax=axes[0]). There's also the subplots=True argument to DataFrame.plot

It should work.

like image 110
Richie F. Avatar answered Dec 02 '25 14:12

Richie F.