0
0
Data Analysis Pythondata~30 mins

FacetGrid for multi-panel views in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
FacetGrid for multi-panel views
📖 Scenario: You work as a data analyst for a car company. You have data about different cars, including their type, fuel efficiency, and horsepower. You want to compare how horsepower relates to fuel efficiency for different types of cars.
🎯 Goal: Build a multi-panel plot using FacetGrid from the seaborn library to visualize the relationship between horsepower and fuel efficiency for each car type.
📋 What You'll Learn
Create a pandas DataFrame called cars with given data
Create a FacetGrid object with cars DataFrame, using car_type as the column facet
Map a scatter plot of horsepower vs mpg on the FacetGrid
Display the plot using plt.show()
💡 Why This Matters
🌍 Real World
Multi-panel plots help compare patterns across groups, such as car types, customer segments, or product categories.
💼 Career
Data analysts and scientists use FacetGrid to explore and present data insights clearly and efficiently.
Progress0 / 4 steps
1
Create the cars DataFrame
Create a pandas DataFrame called cars with these exact columns and values: car_type with ['Sedan', 'SUV', 'Sedan', 'SUV', 'Truck', 'Truck'], horsepower with [130, 250, 115, 300, 210, 230], and mpg with [30, 20, 35, 18, 15, 14].
Data Analysis Python
Hint

Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.

2
Create the FacetGrid object
Import seaborn as sns and matplotlib.pyplot as plt. Then create a FacetGrid object called g using the cars DataFrame, setting col='car_type' to create one panel per car type.
Data Analysis Python
Hint

Use sns.FacetGrid with cars and col='car_type'.

3
Map scatter plot on FacetGrid
Use the map method on the FacetGrid object g to plot a scatter plot with plt.scatter, mapping horsepower on the x-axis and mpg on the y-axis.
Data Analysis Python
Hint

Use g.map(plt.scatter, 'horsepower', 'mpg') to plot points.

4
Display the plot
Use plt.show() to display the multi-panel scatter plot.
Data Analysis Python
Hint

Call plt.show() to display the figure window.