Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Building Multi-Plot Grids | Section
Statistical Visualization with Seaborn
セクション 1.  18
single

single

bookBuilding 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:

  1. Build the grid: you define the "skeleton" (rows and columns) based on your data. At this stage, the plots are empty;
  2. 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.

  1. Set the style to 'whitegrid'. Set the background color to 'cornsilk' ('axes.facecolor').
  2. Initialize the FacetGrid (g):
    • Use the tips dataset (df).
    • Create a column for each 'day'.
    • Create a row for each 'smoker' status.
    • Set the height of each subplot to 3.
  3. Map a histogram onto this grid:
    • Use sns.histplot as the plotting function.
    • Plot the 'total_bill' variable.
    • Set the color to 'olive'.
    • Add a KDE curve (kde=True).
    • Remove the bar filling (fill=False) to see the outline better.
    • Set the binwidth to 4.
  4. Display the plot.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  18
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt