top of page

Data Visualization of Car Fuel Efficiency

  • Writer: saman aboutorab
    saman aboutorab
  • Jan 2, 2024
  • 1 min read

Updated: Jan 8, 2024

What is the relationship between the power of a car's engine ("horsepower") and its fuel efficiency ("mpg")? And how does this relationship vary by the number of cylinders ("cylinders") the car has? Let's find out.


ree

In this project, we'll explore mpg dataset, which contains one row per car model and includes information such as the year the car was made, the number of miles per gallon ("M.P.G.") it achieves, the power of its engine (measured in "horsepower"), and its country of origin.

Import Libraries and data

# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
mpg = pd.read_csv('mpg.csv')
print(mpg.head())
ree

Plot Style

sns.set_style("whitegrid")
sns.set_palette("Spectral")

Horsepower vs. mpg

# Create scatter plot of horsepower vs. mpg
g = sns.relplot(x='horsepower', y='mpg', style='origin', data=mpg, size='cylinders', hue='cylinders')

# Add a title
g.fig.suptitle("Car Horsepower vs. mpg")
plt.show()
ree

Year vs. mpg

# Create line plot
g = sns.relplot(x='model_year', y='mpg', style="origin",
            hue="origin", data=mpg, kind='line')

# Add a title
g.fig.suptitle("Model year vs. mpg")

# Show plot
plt.show()
ree

Model year vs. Horsepower

# Add markers and make each line have the same style
g = sns.relplot(x="model_year", y="horsepower",
            data=mpg, kind="line",
            ci=None, style="origin",
            hue="origin", markers=True, dashes=False)

# Add a title
g.fig.suptitle("Model year vs. Horsepower")

# Show plot
plt.show()
ree

Reference:

Comments


bottom of page