Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Heatmap | Plotten met Seaborn
Ultieme Visualisatie Met Python

book
Heatmap

Note
Definitie

Een heatmap is een methode voor het visualiseren van tweedimensionale gegevens waarbij kleuren worden gebruikt om de grootte van elke waarde weer te geven.

Dit voorbeeld gebruikt een heatmap om pairwise correlaties tussen variabelen in een dataset weer te geven.

Een eenvoudige heatmap maken

seaborn heeft een functie genaamd heatmap(). De enige verplichte parameter is data, die een 2D (rechthoekige) dataset moet zijn.

De meest voorkomende toepassing van een heatmap is met een correlatiematrix, zoals in het bovenstaande voorbeeld. Gegeven een DataFrame, roep eerst de corr()-methode aan om een correlatiematrix te verkrijgen en geef vervolgens deze matrix als argument door aan de functie heatmap():

Een veelvoorkomende toepassing van een heatmap is het weergeven van een correlatiematrix. Gegeven een DataFrame, roep eerst de corr()-methode aan om de correlatiematrix te verkrijgen en geef vervolgens deze matrix als argument door aan de functie heatmap().

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

# Loading the dataset with the countries data
url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv'
countries_df = pd.read_csv(url, index_col=0)

# Creating a correlation matrix with all numeric variables
correlation_matrix = countries_df.corr(numeric_only=True)

# Creating a heatmap based on the correlation matrix
sns.heatmap(correlation_matrix)

plt.show()
123456789101112131415
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # Loading the dataset with the countries data url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' countries_df = pd.read_csv(url, index_col=0) # Creating a correlation matrix with all numeric variables correlation_matrix = countries_df.corr(numeric_only=True) # Creating a heatmap based on the correlation matrix sns.heatmap(correlation_matrix) plt.show()
copy

De correlatiematrix is gemaakt met alleen de numerieke kolommen van de DataFrame. Kolommen met tekstwaarden zijn uitgesloten door numeric_only=True in te stellen.

Annotaties en Kleuren

Deze heatmap kan informatiever worden gemaakt door de juiste waarde (correlatiecoëfficiënt in dit geval) in elke cel te tonen. Dit kan eenvoudig door de parameter annot op True te zetten.

Note
Opmerking

Het is ook mogelijk om de kleuren van onze heatmap aan te passen door de parameter cmap in te stellen (je kunt dit verder verkennen in het "Kleurenpaletten kiezen" artikel).

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

# Loading the dataset with the countries data
url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv'
countries_df = pd.read_csv(url, index_col=0)

# Creating a correlation matrix with all numeric variables
correlation_matrix = countries_df.corr(numeric_only=True)

# Setting annotation and color palette
sns.heatmap(correlation_matrix, annot=True, cmap='viridis')

plt.show()
123456789101112131415
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # Loading the dataset with the countries data url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' countries_df = pd.read_csv(url, index_col=0) # Creating a correlation matrix with all numeric variables correlation_matrix = countries_df.corr(numeric_only=True) # Setting annotation and color palette sns.heatmap(correlation_matrix, annot=True, cmap='viridis') plt.show()
copy

De kleurbalk aan de rechterkant kan worden verwijderd door cbar=False in te stellen.

Note
Meer Bestuderen

In de meeste gevallen is dit alles wat u nodig heeft voor een heatmap-aanpassing, maar u kunt altijd meer ontdekken in de heatmap() documentatie.

Leesbaarheid Verbeteren

Het laatste wat de leesbaarheid van onze heatmap zou verbeteren, is het roteren van de ticks met behulp van de reeds bekende functies xticks() en yticks():

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

# Loading the dataset with the countries data
url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv'
countries_df = pd.read_csv(url, index_col=0)

# Creating a correlation matrix with all numeric variables
correlation_matrix = countries_df.corr(numeric_only=True)

# Creating a heatmap based on the correlation matrix
sns.heatmap(correlation_matrix, annot=True, cmap='viridis')

# Rotating the ticks by 20 degrees counterclockwise
plt.xticks(rotation=20)
plt.yticks(rotation=20)

plt.show()
12345678910111213141516171819
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # Loading the dataset with the countries data url = 'https://content-media-cdn.codefinity.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' countries_df = pd.read_csv(url, index_col=0) # Creating a correlation matrix with all numeric variables correlation_matrix = countries_df.corr(numeric_only=True) # Creating a heatmap based on the correlation matrix sns.heatmap(correlation_matrix, annot=True, cmap='viridis') # Rotating the ticks by 20 degrees counterclockwise plt.xticks(rotation=20) plt.yticks(rotation=20) plt.show()
copy
Taak

Swipe to start coding

  1. Gebruik de juiste methode om een correlatiematrix te maken.
  2. Stel het argument van de methode zo in dat alleen numerieke variabelen worden meegenomen.
  3. Gebruik de juiste functie om een heatmap te maken.
  4. Stel correlation_matrix in als de data voor de heatmap door het als eerste argument te specificeren.
  5. Voeg de waarden in elke cel van de matrix toe door het tweede argument te specificeren.
  6. Stel het palet (kleurenschema) van de heatmap in op 'crest' door het derde (meest rechtse) argument te specificeren.
  7. Draai de x-as- en y-as-ticks 15 graden tegen de klok in door een keyword-argument in xticks() en yticks() te specificeren.

Oplossing

import seaborn as sns
import matplotlib.pyplot as plt

# Loading the dataset with data about the penguins features
penguins_df = sns.load_dataset('penguins')

# Create a correlation matrix with all numeric variables
correlation_matrix = penguins_df.corr(numeric_only=True)

# Create a heatmap based on the correlation matrix
sns.heatmap(correlation_matrix, annot=True, cmap='crest')

# Rotate the ticks by 15 degrees counterclockwise
plt.xticks(rotation=15)
plt.yticks(rotation=15)

plt.show()
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 5. Hoofdstuk 7
import seaborn as sns
import matplotlib.pyplot as plt

# Loading the dataset with data about the penguins features
penguins_df = sns.load_dataset('penguins')

# Create a correlation matrix with all numeric variables
correlation_matrix = ___.___(___=___)

# Create a heatmap based on the correlation matrix
___.___(___, ___=___, ___='___')

# Rotate the ticks by 15 degrees counterclockwise
plt.xticks(___=___)
plt.yticks(___=___)

plt.show()

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

some-alt