简体   繁体   English

如何 plot Pandas 数据框的多列

[英]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.我想 plot 与 MEDV 列的所有其他列。 This code makes all plot on the same graph.此代码使所有 plot 在同一图表上。 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.如果您将axes object 展平,则您的代码将起作用,因为当前您在axes循环一次,这是一个二维 object。 So use axes.flatten() in the for loop and then use ax.scatter which will plot each column to a new figure.因此,在 for 循环中使用axes.flatten() ,然后使用ax.scatter ,它将 plot 每列转换为一个新图形。

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.您需要使用ax.scatter而不是plt.scatter以便它们在您创建的轴中 plot 。

Try plotting with ax[row,col].scatter().尝试使用 ax[row,col].scatter() 进行绘图。 This should do the trick.这应该可以解决问题。 You have to iterate over both, rows and columns then.然后,您必须遍历行和列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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