46

I am trying to make a simple box plot of a variable 'x' contained in two dataframes, df1 and df2. To do this I am using the following code:

fig, axs = plt.subplots()
axs[0, 0].boxplot([df1['x'], df2['x']])
plt.show();

However, I get this:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-108-ce962754d553> in <module>()
----> 2 axs[0, 0].boxplot([df1['x'], df2['x']])
      3 plt.show();
      4 

TypeError: 'AxesSubplot' object is not subscriptable

Any ideas?

0

1 Answer 1

79
fig, axs = plt.subplots()

returns a figure with only one single subplot, so axs already holds it without indexing.

fig, axs = plt.subplots(3)

returns a 1D array of subplots.

fig, axs = plt.subplots(3, 2)

returns a 2D array of subplots.

Note that this is only due to the default setting of the kwarg squeeze=True.
By setting it to False you can force the result to be a 2D-array, independent of the number or arrangement of the subplots.

import numpy
from PIL import Image
import matplotlib.pyplot as plt

imarray = numpy.random.rand(10,10,3) * 255
im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')

#rows = 1; cols = 1;
#rows = 5; cols = 3;
rows = 1; cols = 5;
fig, ax = plt.subplots(rows, cols, squeeze=False)
fig.suptitle('random plots')
i = 0
for r in range(rows):
    for c in range(cols): 
        ax[r][c].imshow(im)
        i = i + 1     
plt.show()
plt.close()
Sign up to request clarification or add additional context in comments.

10 Comments

This is horrible programming practice tho. They are inventing unnecessary edge cases. Now I have to write different code for the case where there is only 1 subplot
@MuhsinFatih I think I can imagine, what you mean. Can you describe, in which concrete way you have to write different code?
This is what I had to add to take care of the edge case: axs = np.array(axs); axs = np.reshape(axs, n). n being the shape variable
It just occured to me that adding this as another answer could be useful. I was initially going to suggest an edit but lets not complicate your answer, it's nicely short and clear
@MuhsinFatih see my edit, this should help
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.