Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

too many indices for array with matplotlib subplots

I’m trying to plot 2 different data sets but I don’t know how to fix the subplot (if i put 2,2 it works but if i try anything else it gives me an error)

fig, axs = plt.subplots(nrows=2, ncols=1)

axs[0,1].plot(adj_close['SOL-USD'])
axs[2,1].set_title('SOL')

plt.show()

error:


----> 6 axs[0,0].plot(adj_close['SOL-USD'])
      7 axs[0,0].set_title('SOL')
      8 axs[0,1].plot(adj_close['ETH-USD'])

TypeError: 'AxesSubplot' object is not subscriptable

IndexError                                Traceback (most recent call last)
Input In [356], in <cell line: 3>()
      1 #ploting the histogram
      2 fig, axs = plt.subplots(2,1,figsize=(16,8),gridspec_kw ={'hspace': 0.2, 'wspace': 0.1})
----> 3 axs[0,0].hist(returns['SOL-USD'], bins=50, range=(-0.2, 0.2))
      4 axs[0,0].set_title('SOL')
      5 axs[1,0].hist(returns['ETH-USD'], bins=50, range=(-0.2, 0.2))

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

what it currently looks like with subplot 2,2:
what it currently looks like with subplot 2,2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

what is going on here is that matplotlib.pyplot.subplots() creates an array of one dimension for axes in fig if nrows or ncols is equal to 1. You can see this by displaying the variable in your current workspace.

>>> fig, axes = plt.subplots(nrows=2, ncols=1)

>>> axes

array([<AxesSubplot:>, <AxesSubplot:>], dtype=object)

That is why an index error appears when trying to call more than one. For more documentation, here is the site to the function matplotlib.pyplot.subplots

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading