2

I am trying to plot a panel of 3 plots using matplotlib and the subplots() method. I have a numpy array of the mean values for the data and a second array for the standard error for the mean values. I tried to create a plot but keep getting an IndexError: too many indices for array.

The numpy array looks like this:

mean_matrix

f1    f2    f3
8.3   4.1   12.9
8.5   4.3   11.2

std_matrix

f1    f2    f3
2.3   0.4   1.2
1.2   0.2  1.3

So here is my code. As you can see, I am trying to plot the mean and then use a fill for 1.96 times the standard error.

f, axarr = plt.subplots(nrows = 1, ncols=3)

axarr[0,0].plot(range(duration), mean_matrix['f1'], subplots=True)
axarr[0,0].fill_between(range(duration), mean_matrix['f1'] +
                    1.96*std_matrix['f1'], mean_matrix['f1'] -
                    1.96*std_matrix['f1'], alpha=0.5)

axarr[0,1].plot(range(duration), mean_matrix['f2'], subplots=True)
axarr[0,1].fill_between(range(duration), mean_matrix['f2'] +
                    1.96*std_matrix['f2'], mean_matrix['f2'] -
                    1.96*std_matrix['f2'], alpha=0.5 )

axarr[0,2].plot(range(duration), mean_matrix['f3'], subplots=True)
axarr[0,2].fill_between(range(duration), mean_matrix['f3'] +
                    1.96*std_matrix['f3'], mean_matrix['f3'] -
                    1.96*std_matrix['f3'], alpha=0.5 )

I get the error: IndexError: too many indices for array

1
  • @all_m sorry for our colliding answers (and thanks for giving it over), I saw that you were delayed by formatting the question:) I added the extra bit of info from your answer to my own, I hope you don't mind (and please let me know if you do). Commented Oct 19, 2015 at 22:50

1 Answer 1

4

If you issue the subplots command:

f, axarr = plt.subplots(nrows = 1, ncols=3)

You will find that

In [7]: axarr.shape
Out[7]: (3,)

In other words, it is a 1d array. Trying to access it with two indices should give an error:

In [4]: axarr
Out[4]: 
array([<matplotlib.axes._subplots.AxesSubplot object at 0x7f0e299dead0>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x7f0e107d3110>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x7f0e1804d5d0>], dtype=object)

In [5]: axarr[0,0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-5-3e75475f2c86> in <module>()
----> 1 axarr[0,0]

IndexError: too many indices

Just try your plot commands with axarr[0] etc.


As @all_m noted, this behaviour is expected if either nrows==1 or ncols==1 in the subplots call, i.e. if you either have a single row or a single column of subplots.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh that is interesting. So I was messing up the rows and columns. @Andras thanks for that insight.
@krishnab, no, you didn't. As @all_m added in a now-deleted answer, if one of your dimensions is singleton (i.e. you have either just a row or just a column of plots) then the resulting axarr will be 1d.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.