Brandon
Brandon

Reputation: 25

Create A Scatterplot from Pandas DataFrame

I'm working on a Pandas DF question and I am having trouble converting some Pandas data into a usable format to create a Scatter Plot.

Here is the code below, please let me know what I am doing wrong and how I can correct it going forward. Honest criticism is needed as I am a beginner.

# 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')

Upvotes: 1

Views: 8103

Answers (2)

Johnnyh101
Johnnyh101

Reputation: 1405

Simply plot x on y as below, where df is your dataframe and x and y are your dependent and independent variables:

import matplotlib.pyplot as plt
import pandas

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

Upvotes: 2

Joe T. Boka
Joe T. Boka

Reputation: 6581

You can use scatter plot from pandas.

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

Upvotes: 0

Related Questions