Solution Review: Visualizing Auto MPG Dataset
This lesson provides the solutions to the previous challenges.
We'll cover the following...
Scatter plot #
Press + to interact
import pandas as pdimport seaborn as sns# Load datadef read_csv():# Define the column names as a listnames = ["mpg", "cylinders", "displacement", "horsepower", "weight", "acceleration", "model_year", "origin", "car_name"]# Read in the CSV file from the webpage using the defined column namesdf = pd.read_csv("auto-mpg.data", header=None, names=names, delim_whitespace=True)return df# Create the scatter plotdef scatter_plot(df):sns.lmplot(x="displacement", y="acceleration", data = df)# Remove excess chart lines and ticks for a nicer looking plotsns.despine()# calling functionscatter_plot(read_csv())
According to the problem statement, we need to find the relationship between acceleration
and displacement
. To plot a visualization, we import seaborn
module at line 2. Before doing it, we have to read the data first. There’s no need to explain how to read the data, as we studied that in detail previously. Dataset is read from line 5 to line 10.
Moving towards the main implementation, look at the header of the scatter_plot(df)
function at line 13. It takes one arguments as input:
df
: A dataframe containing the dataset in the form of a matrix.
Line 14 is the most important line. We are using a built-in function lmplot
from seaborn
library which takes three main argument:
x
: Column whose values are to plotted at the x-axisy
: Column whose values are to plotted at the y-axisdata
Access this course and 1400+ top-rated courses and projects.