I am trying to plot a histogram here but i keep getting an error
numeric_features = ['tenure', 'MonthlyCharges', 'TotalCharges']
cols = 3
rows = int( np.ceil( len(numeric_features)/ cols))
fig, axes = plt.subplots(rows, cols, figsize=(16,16))
for i in range(len(numeric_features)):
this_row = int(np.floor(i/cols))
this_col = int(i%cols)
feat = numeric_features[i]
this_ax = axes[this_row][this_col]
this_ax.hist(df_train[feat], bins=25)
this_ax.set_title(feat)
plt.grid()
plt.show()
It throws the below error:
where am i going wrong here?? i am just trying to plot the numeric values of a dataset.
What is happening here is that when you determine the row values, you get 1
, and as such, the axes is not an array of lists as you expect them to be, it is instead just a list of axes.
Print out your axes and see for yourself. That can be the best way to debug issues like these. Look at the actual variables where errors are occurring to verify your assumptions about structure or type are actually correct. Your method works only for when rows > 1
.
I'll leave you to work out an approach to account for the case when rows = 1
but feel free to respond and I (or anyone else) can offer more advice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.