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

single

bookVisualizing Matrix Data

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

A heatmap is a plot where data values are represented as colors in a matrix.

This is the standard way to visualize correlation matrices (how variables relate to each other) or time series grids (e.g., months vs. years).

Important: unlike scatterplot or barplot which take long lists of data, heatmap typically requires your data to be in a matrix (2D) format. This is often achieved using df.pivot_table() before plotting.

Key Parameters

  • annot=True: writes the data value in each cell;
  • cmap: the color map (gradient) to use. Common choices: 'viridis', 'coolwarm', 'magma';
  • fmt: string formatting code to control how numbers look;
  • 'd': integers (no decimals);
  • '.2f': floats with 2 decimal places;
  • 'g': general format (compact);
  • linewidths / linecolor: adds distinct borders between cells.

Example

Here is a heatmap showing the correlation between numerical variables in the tips dataset.

12345678910111213141516171819
import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('tips') # 1. Calculate Correlation Matrix (creates the grid) corr_matrix = df.corr(numeric_only=True) # 2. Plot Heatmap sns.heatmap( data=corr_matrix, annot=True, # Show numbers fmt='.2f', # 2 decimal places cmap='coolwarm',# Red-Blue gradient linewidths=1 # Separation lines ) plt.show()
copy
タスク

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

Visualize the number of passengers flying over the years. The data has already been reshaped into a matrix (upd_df) for you using pivot_table.

  1. Set the style to 'ticks'. Change the figure background color to 'seagreen' ('figure.facecolor').
  2. Create a heatmap:
    • Pass upd_df as the data (this is the first argument, so you don't need data=).
    • Use the 'viridis' color map (cmap).
    • Display the numbers in the cells (annot=True).
    • Format the numbers using '0.99g' (general format).
    • Set the color of the lines between cells to 'plum' (linecolor).
  3. Display the plot.

解答

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

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

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

セクション 1.  16
single

single

AIに質問する

expand

AIに質問する

ChatGPT

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

some-alt