Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Heatmap | Plotting with Seaborn
Ultimate Visualization with Python
course content

Зміст курсу

Ultimate Visualization with Python

Ultimate Visualization with Python

1. Matplotlib Introduction
2. Creating Commonly Used Plots
3. Plots Customization
4. More Statistical Plots
5. Plotting with Seaborn

bookHeatmap

A heatmap is a method for visualizing two-dimensional data using colors to represent the magnitude of each value. Here is an example of a heatmap:

In this example we used a heatmap to represent pairwise correlation between the variables in a dataset. With such a heatmap everything is neat and clear, exactly what we need from a good visulization.

Creating a Simple Heatmap

seaborn has a function called heatmap(). Its only required parameter is data which should be a 2D (rectangle) dataset.

Perhaps the most common use case of a heatmap is with a correlation matrix like in the example above. Given a DataFrame, we should first call its corr() method to get a correlation matrix and only then pass this matrix as an argument for the heatmap() function:

1234567891011
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' # Loading the dataset with the countries data 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

Everything is pretty straightforward here, however, we didn’t use all the columns of the DataFrame to create a correlation matrix (numeric_only=True), since they have strings and are not numeric.

Annotation and Colors

This heatmap can be made more informative via writing the appropriate value (correlation coefficient in our case) in each cell . That can be done simply by setting the annot parameter to True.

It is also possible to change the colors for our heatmap via setting the cmap parameter (you can explore the palettes here).

With this in mind, let’s now modify our example:

1234567891011
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' # Loading the dataset with the countries data 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

We could also remove the color bar on the right by setting cbar=False.

In most of the cases that’s all you will need from a heatmap customization, however, you can always explore more in its documentation.

Improving Readability

The final thing that would improve the readability of our heatmap is rotating the ticks using already familiar xticks() and yticks() functions:

1234567891011121314
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' # Loading the dataset with the countries data 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

Завдання

  1. Use the correct method to create a correlation matrix.
  2. Set the argument of the method to include only numeric variables.
  3. Use the correct function to create a heatmap.
  4. Set correlation_matrix to be the data for the heatmap via specifying the first argument.
  5. Add the values in each cell of the matrix via specifying the second argument.
  6. Set the palette (color map) of the heatmap to 'crest' via specifying the third (rightmost) argument.
  7. Rotate x-axis and y-axis ticks by 15 degrees counterclockwise via specifying a keyword argument in xticks() and yticks().

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 7
toggle bottom row

bookHeatmap

A heatmap is a method for visualizing two-dimensional data using colors to represent the magnitude of each value. Here is an example of a heatmap:

In this example we used a heatmap to represent pairwise correlation between the variables in a dataset. With such a heatmap everything is neat and clear, exactly what we need from a good visulization.

Creating a Simple Heatmap

seaborn has a function called heatmap(). Its only required parameter is data which should be a 2D (rectangle) dataset.

Perhaps the most common use case of a heatmap is with a correlation matrix like in the example above. Given a DataFrame, we should first call its corr() method to get a correlation matrix and only then pass this matrix as an argument for the heatmap() function:

1234567891011
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' # Loading the dataset with the countries data 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

Everything is pretty straightforward here, however, we didn’t use all the columns of the DataFrame to create a correlation matrix (numeric_only=True), since they have strings and are not numeric.

Annotation and Colors

This heatmap can be made more informative via writing the appropriate value (correlation coefficient in our case) in each cell . That can be done simply by setting the annot parameter to True.

It is also possible to change the colors for our heatmap via setting the cmap parameter (you can explore the palettes here).

With this in mind, let’s now modify our example:

1234567891011
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' # Loading the dataset with the countries data 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

We could also remove the color bar on the right by setting cbar=False.

In most of the cases that’s all you will need from a heatmap customization, however, you can always explore more in its documentation.

Improving Readability

The final thing that would improve the readability of our heatmap is rotating the ticks using already familiar xticks() and yticks() functions:

1234567891011121314
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' # Loading the dataset with the countries data 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

Завдання

  1. Use the correct method to create a correlation matrix.
  2. Set the argument of the method to include only numeric variables.
  3. Use the correct function to create a heatmap.
  4. Set correlation_matrix to be the data for the heatmap via specifying the first argument.
  5. Add the values in each cell of the matrix via specifying the second argument.
  6. Set the palette (color map) of the heatmap to 'crest' via specifying the third (rightmost) argument.
  7. Rotate x-axis and y-axis ticks by 15 degrees counterclockwise via specifying a keyword argument in xticks() and yticks().

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 7
toggle bottom row

bookHeatmap

A heatmap is a method for visualizing two-dimensional data using colors to represent the magnitude of each value. Here is an example of a heatmap:

In this example we used a heatmap to represent pairwise correlation between the variables in a dataset. With such a heatmap everything is neat and clear, exactly what we need from a good visulization.

Creating a Simple Heatmap

seaborn has a function called heatmap(). Its only required parameter is data which should be a 2D (rectangle) dataset.

Perhaps the most common use case of a heatmap is with a correlation matrix like in the example above. Given a DataFrame, we should first call its corr() method to get a correlation matrix and only then pass this matrix as an argument for the heatmap() function:

1234567891011
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' # Loading the dataset with the countries data 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

Everything is pretty straightforward here, however, we didn’t use all the columns of the DataFrame to create a correlation matrix (numeric_only=True), since they have strings and are not numeric.

Annotation and Colors

This heatmap can be made more informative via writing the appropriate value (correlation coefficient in our case) in each cell . That can be done simply by setting the annot parameter to True.

It is also possible to change the colors for our heatmap via setting the cmap parameter (you can explore the palettes here).

With this in mind, let’s now modify our example:

1234567891011
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' # Loading the dataset with the countries data 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

We could also remove the color bar on the right by setting cbar=False.

In most of the cases that’s all you will need from a heatmap customization, however, you can always explore more in its documentation.

Improving Readability

The final thing that would improve the readability of our heatmap is rotating the ticks using already familiar xticks() and yticks() functions:

1234567891011121314
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' # Loading the dataset with the countries data 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

Завдання

  1. Use the correct method to create a correlation matrix.
  2. Set the argument of the method to include only numeric variables.
  3. Use the correct function to create a heatmap.
  4. Set correlation_matrix to be the data for the heatmap via specifying the first argument.
  5. Add the values in each cell of the matrix via specifying the second argument.
  6. Set the palette (color map) of the heatmap to 'crest' via specifying the third (rightmost) argument.
  7. Rotate x-axis and y-axis ticks by 15 degrees counterclockwise via specifying a keyword argument in xticks() and yticks().

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

A heatmap is a method for visualizing two-dimensional data using colors to represent the magnitude of each value. Here is an example of a heatmap:

In this example we used a heatmap to represent pairwise correlation between the variables in a dataset. With such a heatmap everything is neat and clear, exactly what we need from a good visulization.

Creating a Simple Heatmap

seaborn has a function called heatmap(). Its only required parameter is data which should be a 2D (rectangle) dataset.

Perhaps the most common use case of a heatmap is with a correlation matrix like in the example above. Given a DataFrame, we should first call its corr() method to get a correlation matrix and only then pass this matrix as an argument for the heatmap() function:

1234567891011
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' # Loading the dataset with the countries data 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

Everything is pretty straightforward here, however, we didn’t use all the columns of the DataFrame to create a correlation matrix (numeric_only=True), since they have strings and are not numeric.

Annotation and Colors

This heatmap can be made more informative via writing the appropriate value (correlation coefficient in our case) in each cell . That can be done simply by setting the annot parameter to True.

It is also possible to change the colors for our heatmap via setting the cmap parameter (you can explore the palettes here).

With this in mind, let’s now modify our example:

1234567891011
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' # Loading the dataset with the countries data 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

We could also remove the color bar on the right by setting cbar=False.

In most of the cases that’s all you will need from a heatmap customization, however, you can always explore more in its documentation.

Improving Readability

The final thing that would improve the readability of our heatmap is rotating the ticks using already familiar xticks() and yticks() functions:

1234567891011121314
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd url = 'https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/47339f29-4722-4e72-a0d4-6112c70ff738/countries_data.csv' # Loading the dataset with the countries data 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

Завдання

  1. Use the correct method to create a correlation matrix.
  2. Set the argument of the method to include only numeric variables.
  3. Use the correct function to create a heatmap.
  4. Set correlation_matrix to be the data for the heatmap via specifying the first argument.
  5. Add the values in each cell of the matrix via specifying the second argument.
  6. Set the palette (color map) of the heatmap to 'crest' via specifying the third (rightmost) argument.
  7. Rotate x-axis and y-axis ticks by 15 degrees counterclockwise via specifying a keyword argument in xticks() and yticks().

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 5. Розділ 7
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt