I have created 6 pie charts, however, they all appear separately. I would like to have them all appear on the same figure (3 rows, 2 columns).
I have therefore tried to use the subplot function, but I don't think I am using it correctly and I do not see where I am going wrong.
Below is the code I wrote, using python 2.7. I am new to coding and just need a bit of guidance for this trivial matter, thank you!
Also, if you have any tips on making this code more "streamlined", I'm all ears!
I get the error:
AttributeError: 'numpy.ndarray' object has no attribute 'pie'
CodePudding user response:
The fastest way to get your code to run is to "flatten" the 2D axs
array into a 1D array right after creating it:
fig, axs = plt.subplots(3, 2)
axs = axs.flatten()
# The rest of your existing code
Also, indexing in Python starts at 0, so you'll want to change your index numbers to go from 0 to 5, instead of 1 to 6.
Explanation: the error you're getting comes from fact that the axis array returned by fig, axs = plt.subplots(3, 2)
is two-dimensional, with 3 rows and 2 columns. So the first AxesSubplot
object is at axs[0, 0]
(or axs[0][0]
), but axs[0]
returns the first row of the 3-by-2 array, which is itself an array.
(On another note, you should upgrade to Python 3.x :) https://www.python.org/doc/sunset-python-2/)