从 Pandas DataFrame 创建散点图

Create A Scatterplot from Pandas DataFrame

我正在研究一个 Pandas DF 问题,我在将一些 Pandas 数据转换为可用格式以创建散点图时遇到问题。

这是下面的代码,请让我知道我做错了什么以及我如何才能更正它。因为我是初学者,所以需要诚实的批评。

# Import Data
df = pd.read_csv(filepath + 'BaltimoreData.csv')

df = df.dropna()
print(df.head(20))
# These are two categories within the data
df.plot(df['Bachelors degree'], df['Median Income'])

# Plotting the Data
df.plot(kind = 'scatter', x = 'Bachelor degree', y = 'Median Income')
df.plot(kind = 'density')

您可以使用 pandas 中的 scatter plot

import pandas
import matplotlib.pyplot as plt
plt.style.use('ggplot')
df.plot.scatter(x='Bachelors degree', y='Median Income');
plt.show()

如下所示简单地在 y 上绘制 x,其中 df 是您的数据框,x 和 y 是您的因变量和自变量:

import matplotlib.pyplot as plt
import pandas

plt.scatter(x=df['Bachelors degree'], y=df['Median Income'])
plt.show()