I want to generate several subplots within a for loop, but always get the AttributeError: 'numpy.ndarray' object has no attribute 'plot'
.
measurements = 9
fig, axs = plt.subplots(3,(measurements 2)//3, sharey=False, gridspec_kw={'wspace': 0.1},figsize =(20, 10))
for i in range(0,measurements):
try:
axs[0].plot(dfrld_1['RLD date(' str(i) ')'], dfrld_1['Depth'],lw=2, color=next(red1))
axs[1].plot(dfrld_2['RLD date(' str(i) ')'], dfrld_2['Depth'],lw=2, color=next(red1))
axs[2].plot(dfrld_3['RLD date(' str(i) ')'], dfrld_3['Depth'],lw=2, color=next(red1))
axs[3].plot(dfrld_4['RLD date(' str(i) ')'], dfrld_4['Depth'],lw=2, color=next(red1))
axs[4].plot(dfrld_5['RLD date(' str(i) ')'], dfrld_5['Depth'],lw=2, color=next(red1))
axs[5].plot(dfrld_6['RLD date(' str(i) ')'], dfrld_6['Depth'],lw=2, color=next(red1))
axs[6].plot(dfrld_7['RLD date(' str(i) ')'], dfrld_7['Depth'],lw=2, color=next(red1))
axs[7].plot(dfrld_8['RLD date(' str(i) ')'], dfrld_8['Depth'],lw=2, color=next(red1))
axs[8].plot(dfrld_9['RLD date(' str(i) ')'], dfrld_9['Depth'],lw=2, color=next(red1))
except KeyError:
pass
plt.show()
All dataframes (dfrld_1, dfrld2,...)
have the same format.
Depth RLD date(1) RLD date(2) RLD date(3) RLD date(4) RLD date(5) RLD date(6) RLD date(7) RLD date(8) RLD date(9)
0 -10 0.0 0.0 0.135132 0.697177 0.815027 0.571775 0.467736 0.152040 0.168436
1 -20 0.0 0.0 0.059295 0.286268 0.383652 0.218391 0.155417 0.065664 0.057106
2 -40 0.0 0.0 0.000000 0.052796 0.151876 0.297082 0.304435 0.268104 0.247111
3 -60 0.0 0.0 0.000000 0.000000 0.090140 0.145970 0.196542 0.148141 0.145210
4 -80 0.0 0.0 0.000000 0.000000 0.000000 0.000000 0.135144 0.124805 0.114642
The exact error I get is:
AttributeError: 'numpy.ndarray' object has no attribute 'plot'
With anoather dataframe, which look the same and only 3 subplots this is working without any problems. But here I cannot find my mistake.
CodePudding user response:
According to the docs, numpy.ndarray
does not have a plot
method, which is a method of pyplot
. You call subplots
, which, according to the docs, returns axes.Axes
or array of axes. Since there can be two different return types, it should be clear what the cause of your problem is. You will need to handle the case when you receive an array as the return.
You may want to read about plotting an array at sources like this one: https://www.codegrepper.com/code-examples/python/how to plot numpy array in matplotlib
WARNING: I have no experience in Python or MatPlotLib whatsoever, all the information I have given in this answer is purely theoretical and based on the articles I have found in the documentation when I have searched for concepts with the intent of helping you.
CodePudding user response:
The problem is that axs
is a numpy array with shape (3,3) and not (9, 1) as you expect I think in your code.
Try:
measurements = 9
fig, axs = plt.subplots(3,(measurements 2)//3, sharey=False, gridspec_kw={'wspace': 0.1},figsize =(20, 10))
for i in range(0,measurements):
try:
axs[0][0].plot(dfrld_1['RLD date(' str(i) ')'], dfrld_1['Depth'],lw=2, color=next(red1))
axs[0][1].plot(dfrld_2['RLD date(' str(i) ')'], dfrld_2['Depth'],lw=2, color=next(red1))
axs[0][2].plot(dfrld_3['RLD date(' str(i) ')'], dfrld_3['Depth'],lw=2, color=next(red1))
axs[1][0].plot(dfrld_4['RLD date(' str(i) ')'], dfrld_4['Depth'],lw=2, color=next(red1))
axs[1][1].plot(dfrld_5['RLD date(' str(i) ')'], dfrld_5['Depth'],lw=2, color=next(red1))
axs[1][2].plot(dfrld_6['RLD date(' str(i) ')'], dfrld_6['Depth'],lw=2, color=next(red1))
axs[2][0].plot(dfrld_7['RLD date(' str(i) ')'], dfrld_7['Depth'],lw=2, color=next(red1))
axs[2][1].plot(dfrld_8['RLD date(' str(i) ')'], dfrld_8['Depth'],lw=2, color=next(red1))
axs[2][2].plot(dfrld_9['RLD date(' str(i) ')'], dfrld_9['Depth'],lw=2, color=next(red1))
except KeyError:
pass
plt.show()