简体   繁体   中英

how to plot many columns of Pandas data frame

I need to make scatter plots using Boston Housing Dataset. I want to plot all the other columns with MEDV column. This code makes all plot on the same graph. How can I separate them? enter image description here

import matplotlib.pyplot as plt
%matplotlib inline
fig, axes = plt.subplots(nrows=3, ncols=2, figsize=(12, 12))
for column, ax in zip(['CRIM', 'ZN','INDUS', 'CHAS', 'NOX', 'RM'], axes):
    plt.scatter(boston_df[column], boston_df.MEDV)

Your code will work if you flatten the axes object because currently you are looping once over axes which is a 2-d object. So use axes.flatten() in the for loop and then use ax.scatter which will plot each column to a new figure.

The order of plotting will be the first row, then the second row and then the third row

fig, axes = plt.subplots(nrows=3, ncols=2, figsize=(12, 12))
for column, ax in zip(['CRIM', 'ZN','INDUS', 'CHAS', 'NOX', 'RM'], axes.flatten()):
    ax.scatter(boston_df[column], boston_df.MEDV)

You need to use ax.scatter instead of plt.scatter so that they plot in the axes you've created.

Try plotting with ax[row,col].scatter(). This should do the trick. You have to iterate over both, rows and columns then.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM