セクション 1. 章 18
single
Building Multi-Plot Grids
メニューを表示するにはスワイプしてください
A FacetGrid is the fundamental structure that allows you to create a matrix of plots defined by categorical variables.
Think of it as a two-step process:
- Build the grid: you define the "skeleton" (rows and columns) based on your data. At this stage, the plots are empty;
- Map the data: you use the
.map()method to "paint" a specific type of plot (histogram, scatter, etc.) onto every cell in that grid.
The Workflow
# Step 1: Initialize the grid
# This creates empty subplots: one for each 'time' (Lunch/Dinner)
g = sns.FacetGrid(data=df, col='time')
# Step 2: Map a plot onto the grid
# This draws a histogram of 'total_bill' in every empty subplot
g.map(sns.histplot, 'total_bill')
Key Parameters
col/row: the variables that define the grid structure;height: height (in inches) of each individual facet (subplot);.map(func, *args, **kwargs):func: the plotting function to use (e.g.,sns.histplot,plt.scatter);*args: the column names to plot (e.g.,'total_bill');**kwargs: styling arguments (e.g.,color='red').
タスク
スワイプしてコーディングを開始
Analyze the distribution of total bills, breaking it down by day and smoking status.
- Set the style to
'whitegrid'. Set the background color to'cornsilk'('axes.facecolor'). - Initialize the FacetGrid (
g):- Use the
tipsdataset (df). - Create a column for each
'day'. - Create a row for each
'smoker'status. - Set the
heightof each subplot to3.
- Use the
- Map a histogram onto this grid:
- Use
sns.histplotas the plotting function. - Plot the
'total_bill'variable. - Set the
colorto'olive'. - Add a KDE curve (
kde=True). - Remove the bar filling (
fill=False) to see the outline better. - Set the
binwidthto4.
- Use
- Display the plot.
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 1. 章 18
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください