Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Plotting Pairwise Relationships | Section
Statistical Visualization with Seaborn
セクション 1.  19
single

single

bookPlotting Pairwise Relationships

メニューを表示するにはスワイプしてください

The PairGrid is a subplot grid for plotting pairwise relationships in a dataset.

It creates a matrix of axes where each variable in the dataset is shared across a row and a column.

  • Diagonal: shows the univariate distribution of a single variable (since x=y);
  • Off-Diagonal: shows the bivariate relationship between two different variables.

Controlling the Grid

Unlike pairplot (which is fully automatic), PairGrid requires you to map plots to specific sections explicitly.

  • g.map_diag(func): plots on the diagonal (e.g., sns.histplot);
  • g.map_offdiag(func): plots on all non-diagonal cells (e.g., sns.scatterplot);
  • g.map_upper(func) / g.map_lower(func): plots specifically in the upper or lower triangle of the grid.

Example

Here we create a grid where the diagonal shows histograms and the lower triangle shows density contours.

123456789101112131415
import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('penguins') # 1. Initialize the grid g = sns.PairGrid(df, hue='species') # 2. Map plots to specific regions g.map_diag(sns.histplot) # Diagonal: Histograms g.map_offdiag(sns.scatterplot) # Off-diagonal: Scatterplots g.add_legend() plt.show()
copy
タスク

スワイプしてコーディングを開始

Create a customized grid to analyze the relationships between penguin measurements.

  1. Set the style to 'ticks'. Change the figure background color to 'lightpink' ('figure.facecolor').
  2. Initialize the PairGrid (g):
    • Use the df dataset.
    • Color the data points by 'species' (hue).
    • Use the 'rocket_r' palette.
    • Set diag_sharey=False (this allows diagonal plots to have their own Y-axis scale).
  3. Diagonal plots: map sns.histplot to the diagonal using .map_diag(). Add a KDE curve (kde=True).
  4. Off-diagonal plots: map sns.scatterplot to the rest of the grid using .map_offdiag(). Set the point border width (linewidth) to 0.9 and the border color (edgecolor) to 'purple'.
  5. Add the legend and display the plot.

解答

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

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

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

セクション 1.  19
single

single

AIに質問する

expand

AIに質問する

ChatGPT

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

some-alt