đŸ•·ïž Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 88 (from laksa192)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

â„č Skipped - page is already crawled

📄
INDEXABLE
✅
CRAWLED
23 days ago
đŸ€–
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.8 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://likegeeks.com/nan-values-seaborn-heatmap/
Last Crawled2026-03-22 10:17:11 (23 days ago)
First Indexed2024-02-08 17:08:05 (2 years ago)
HTTP Status Code200
Meta TitleHandle NaN values in Seaborn heatmap
Meta DescriptionLearn how to handle NaNs in Seaborn heatmaps with this tutorial. Learn to drop, fill, interpolate, and visually distinguish NaNs in real-world data.
Meta Canonicalnull
Boilerpipe Text
One common challenge faced when creating heatmaps is the presence of NaN (Not a Number) values. These NaNs can impact the visual output, leading to misleading interpretations or an unclear representation of the data. In this tutorial, you’ll learn how to handle NaN values in Seaborn heatmaps . Whether you are dealing with sparse NaNs that can be easily dropped, or require more nuanced approaches like filling, interpolating, or visually distinguishing NaNs, this tutorial covers it all. 1 Remove NaN Values 2 Fill NaNs with a Specific Value 3 Interpolate NaNs 4 Mask NaNs in the Heatmap 5 Use a Different Color for NaNs Remove NaN Values In scenarios where NaNs are sparse and not critical to your analysis, one effective method is to remove them using the dropna() method. First, you’ll need to import the necessary libraries and create a sample dataset. import seaborn as sns import pandas as pd import numpy as np data = { 'Call Quality': [3.5, np.nan, 4.2, 3.8], 'Internet Speed': [50, 45, np.nan, 60], 'Customer Satisfaction': [4.0, 3.6, np.nan, 4.5] } df = pd.DataFrame(data) print(df) Output: Call Quality Internet Speed Customer Satisfaction 0 3.5 50.0 4.0 1 NaN 45.0 3.6 2 4.2 NaN NaN 3 3.8 60.0 4.5 Note the NaN values in the dataset. Next, let’s remove these NaN values: cleaned_df = df.dropna() print(cleaned_df) Output: Call Quality Internet Speed Customer Satisfaction 0 3.5 50.0 4.0 3 3.8 60.0 4.5 In the above step, dropna() removes rows with any NaN values. Let’s plot the heatmap using Seaborn after we’ve dropped the NaN values. This will help visualize how the removal of NaN values impacts the heatmap representation. import matplotlib.pyplot as plt plt.figure(figsize=(8, 4)) sns.heatmap(cleaned_df, annot=True, cmap='viridis') plt.title('Heatmap (Without NaN Values)') plt.show() Output: Fill NaNs with a Specific Value In certain situations, you want to retain the original size of your dataset, especially when the presence of each row is significant for your analysis. In such cases, rather than removing NaN values, you can fill them with a specific value. The .fillna() function in Pandas allows you to do this. filled_df = df.fillna(0) print(filled_df) Output: Call Quality Internet Speed Customer Satisfaction 0 3.5 50.0 4.0 1 0.0 45.0 3.6 2 4.2 0.0 0.0 3 3.8 60.0 4.5 In the code above, fillna(0) replaces all NaN values in the dataset with 0 . Now, let’s create a heatmap with the NaN values filled: plt.figure(figsize=(8, 4)) sns.heatmap(filled_df, annot=True, cmap='viridis') plt.title('Heatmap (NaNs Filled with 0)') plt.show() Output: Interpolate NaNs Interpolation is another method to handle NaN values in datasets, especially when a linear relationship can be assumed between data points. By interpolating, you estimate the NaN values based on neighboring data points. This method is useful in time-series data or when the data points have a logical sequence. interpolated_df = df.interpolate() print(interpolated_df) Output: Call Quality Internet Speed Customer Satisfaction 0 3.50 50.0 4.00 1 3.85 45.0 3.60 2 4.20 52.5 4.05 3 3.80 60.0 4.50 Here, interpolate() function calculates the NaN values by estimating them based on adjacent values. For example, in the ‘Call Quality’ column, the NaN value is replaced with an average of its neighboring values. Now, let’s visualize this interpolated data using a heatmap: # Plotting the heatmap with interpolated values plt.figure(figsize=(8, 4)) sns.heatmap(interpolated_df, annot=True, cmap='viridis') plt.title('Heatmap (Interpolated NaN Values)') plt.show() Output: Mask NaNs in the Heatmap Masking NaNs in a heatmap allows you to visually distinguish these values from the rest of the data. This method is useful when you want to maintain the original dataset’s structure, including the NaN values, while still providing a clear visual representation of where data is missing. nan_mask = df.isna() plt.figure(figsize=(8, 4)) sns.heatmap(df, annot=True, cmap='viridis', mask=nan_mask) plt.title('Heatmap (NaNs Masked)') plt.show() Output: The masked areas do not have annotations and color, clearly indicating the absence of data. Use a Different Color for NaNs Another method to handle NaNs in heatmaps is using a different color for these values. This method highlights the NaNs distinctly and makes them easily identifiable. First, we’ll prepare a colormap that distinguishes NaNs: from matplotlib.colors import ListedColormap # Custom colormap: NaNs will be shown in grey cmap = ListedColormap(sns.color_palette("viridis", as_cmap=True).colors + [(0.75, 0.75, 0.75)]) # Preparing the data: NaNs are set to a unique number unique_number_for_nans = -1 heatmap_data = df.fillna(unique_number_for_nans) plt.figure(figsize=(8, 4)) sns.heatmap(heatmap_data, annot=True, cmap=cmap, cbar=False) plt.title('Heatmap (Different Color for NaNs)') plt.show() Output: Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.
Markdown
[Skip to content](https://likegeeks.com/nan-values-seaborn-heatmap/#primary) [![Handle NaN values in Seaborn heatmap](https://likegeeks.com/wp-content/uploads/2020/12/logo.png)](https://likegeeks.com/) Menu - [Home](https://likegeeks.com/ "Home") - [Linux](https://likegeeks.com/linux/) - [Linux Commands](https://likegeeks.com/linux/linux-commands/) - [Bash Scripting](https://likegeeks.com/linux/bash-scripting/) - [Server Administration](https://likegeeks.com/server-administration/) - [Web Development](https://likegeeks.com/web-development/) - [Python](https://likegeeks.com/python/) - [NumPy](https://likegeeks.com/python/numpy/) - [Pandas](https://likegeeks.com/python/pandas/) - [Seaborn](https://likegeeks.com/python/seaborn/) - [Tools](https://likegeeks.com/nan-values-seaborn-heatmap/) - [Compare Lists](https://likegeeks.com/compare-two-lists/) - [JSON To Table](https://likegeeks.com/json-to-table/) - [JSON to Markdown](https://likegeeks.com/json-to-markdown/) - [XML To Excel](https://likegeeks.com/xml-to-excel/) [Home](https://likegeeks.com/) » [Python](https://likegeeks.com/python/) » [Seaborn](https://likegeeks.com/python/seaborn/) # Handle NaN values in Seaborn heatmap [Mokhtar Ebrahim](https://likegeeks.com/author/admin/) Last Updated On: [February 8, 2024](https://likegeeks.com/nan-values-seaborn-heatmap/) One common challenge faced when creating heatmaps is the presence of NaN (Not a Number) values. These NaNs can impact the visual output, leading to misleading interpretations or an unclear representation of the data. In this tutorial, you’ll learn how to handle NaN values in [Seaborn heatmaps](https://likegeeks.com/seaborn-heatmap-tutorial/). Whether you are dealing with sparse NaNs that can be easily dropped, or require more nuanced approaches like filling, interpolating, or visually distinguishing NaNs, this tutorial covers it all. **Table of Contents** [hide](https://likegeeks.com/nan-values-seaborn-heatmap/) - [1 Remove NaN Values](https://likegeeks.com/nan-values-seaborn-heatmap/#Remove_NaN_Values) - [2 Fill NaNs with a Specific Value](https://likegeeks.com/nan-values-seaborn-heatmap/#Fill_NaNs_with_a_Specific_Value) - [3 Interpolate NaNs](https://likegeeks.com/nan-values-seaborn-heatmap/#Interpolate_NaNs) - [4 Mask NaNs in the Heatmap](https://likegeeks.com/nan-values-seaborn-heatmap/#Mask_NaNs_in_the_Heatmap) - [5 Use a Different Color for NaNs](https://likegeeks.com/nan-values-seaborn-heatmap/#Use_a_Different_Color_for_NaNs) ## Remove NaN Values In scenarios where NaNs are sparse and not critical to your analysis, one effective method is to remove them using the `dropna()` method. First, you’ll need to import the necessary libraries and create a sample dataset. ``` import seaborn as sns import pandas as pd import numpy as np data = { 'Call Quality': [3.5, np.nan, 4.2, 3.8], 'Internet Speed': [50, 45, np.nan, 60], 'Customer Satisfaction': [4.0, 3.6, np.nan, 4.5] } df = pd.DataFrame(data) print(df) ``` Output: ``` Call Quality Internet Speed Customer Satisfaction 0 3.5 50.0 4.0 1 NaN 45.0 3.6 2 4.2 NaN NaN 3 3.8 60.0 4.5 ``` Note the NaN values in the dataset. Next, let’s remove these NaN values: ``` cleaned_df = df.dropna() print(cleaned_df) ``` Output: ``` Call Quality Internet Speed Customer Satisfaction 0 3.5 50.0 4.0 3 3.8 60.0 4.5 ``` In the above step, `dropna()` removes rows with any NaN values. Let’s plot the heatmap using Seaborn after we’ve dropped the NaN values. This will help visualize how the removal of NaN values impacts the heatmap representation. ``` import matplotlib.pyplot as plt plt.figure(figsize=(8, 4)) sns.heatmap(cleaned_df, annot=True, cmap='viridis') plt.title('Heatmap (Without NaN Values)') plt.show() ``` Output: ![Remove NaN Values](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20800%20400'%3E%3C/svg%3E)![Remove NaN Values](https://likegeeks.com/wp-content/uploads/2024/02/01-Remove-NaN-Values.jpeg) ## Fill NaNs with a Specific Value In certain situations, you want to retain the original size of your dataset, especially when the presence of each row is significant for your analysis. In such cases, rather than removing NaN values, you can fill them with a specific value. The [`.fillna()` function](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.fillna.html) in Pandas allows you to do this. ``` filled_df = df.fillna(0) print(filled_df) ``` Output: ``` Call Quality Internet Speed Customer Satisfaction 0 3.5 50.0 4.0 1 0.0 45.0 3.6 2 4.2 0.0 0.0 3 3.8 60.0 4.5 ``` In the code above, `fillna(0)` replaces all NaN values in the dataset with `0`. Now, let’s create a heatmap with the NaN values filled: ``` plt.figure(figsize=(8, 4)) sns.heatmap(filled_df, annot=True, cmap='viridis') plt.title('Heatmap (NaNs Filled with 0)') plt.show() ``` Output: ![Fill NaNs with a Specific Value](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20800%20400'%3E%3C/svg%3E)![Fill NaNs with a Specific Value](https://likegeeks.com/wp-content/uploads/2024/02/02-Fill-NaNs-with-a-Specific-Value.jpeg) ## Interpolate NaNs Interpolation is another method to handle NaN values in datasets, especially when a linear relationship can be assumed between data points. By interpolating, you estimate the NaN values based on neighboring data points. This method is useful in time-series data or when the data points have a logical sequence. ``` interpolated_df = df.interpolate() print(interpolated_df) ``` Output: ``` Call Quality Internet Speed Customer Satisfaction 0 3.50 50.0 4.00 1 3.85 45.0 3.60 2 4.20 52.5 4.05 3 3.80 60.0 4.50 ``` Here, [`interpolate()` function](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.interpolate.html) calculates the NaN values by estimating them based on adjacent values. For example, in the ‘Call Quality’ column, the NaN value is replaced with an average of its neighboring values. Now, let’s visualize this interpolated data using a heatmap: ``` # Plotting the heatmap with interpolated values plt.figure(figsize=(8, 4)) sns.heatmap(interpolated_df, annot=True, cmap='viridis') plt.title('Heatmap (Interpolated NaN Values)') plt.show() ``` Output: ![Interpolate NaNs](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20800%20400'%3E%3C/svg%3E)![Interpolate NaNs](https://likegeeks.com/wp-content/uploads/2024/02/03-Interpolate-NaNs.jpeg) ## Mask NaNs in the Heatmap Masking NaNs in a heatmap allows you to visually distinguish these values from the rest of the data. This method is useful when you want to maintain the original dataset’s structure, including the NaN values, while still providing a clear visual representation of where data is missing. ``` nan_mask = df.isna() plt.figure(figsize=(8, 4)) sns.heatmap(df, annot=True, cmap='viridis', mask=nan_mask) plt.title('Heatmap (NaNs Masked)') plt.show() ``` Output: ![Mask NaNs in the Heatmap](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20800%20400'%3E%3C/svg%3E)![Mask NaNs in the Heatmap](https://likegeeks.com/wp-content/uploads/2024/02/04-Mask-NaNs-in-the-Heatmap.jpeg) The masked areas do not have annotations and color, clearly indicating the absence of data. ## Use a Different Color for NaNs Another method to handle NaNs in heatmaps is using a different color for these values. This method highlights the NaNs distinctly and makes them easily identifiable. First, we’ll prepare a colormap that distinguishes NaNs: ``` from matplotlib.colors import ListedColormap # Custom colormap: NaNs will be shown in grey cmap = ListedColormap(sns.color_palette("viridis", as_cmap=True).colors + [(0.75, 0.75, 0.75)]) # Preparing the data: NaNs are set to a unique number unique_number_for_nans = -1 heatmap_data = df.fillna(unique_number_for_nans) plt.figure(figsize=(8, 4)) sns.heatmap(heatmap_data, annot=True, cmap=cmap, cbar=False) plt.title('Heatmap (Different Color for NaNs)') plt.show() ``` Output: ![Use a Different Color for NaNs](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20800%20400'%3E%3C/svg%3E)![Use a Different Color for NaNs](https://likegeeks.com/wp-content/uploads/2024/02/05-Use-a-Different-Color-for-NaNs.jpeg) - [Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https://likegeeks.com/nan-values-seaborn-heatmap/) - [Tweet on Twitter](https://twitter.com/intent/tweet?text=Handle+NaN+values+in+Seaborn+heatmap&url=https://likegeeks.com/nan-values-seaborn-heatmap/&via=likegeeks) ![Mokhtar Ebrahim](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20100%20100'%3E%3C/svg%3E) ![Mokhtar Ebrahim](https://secure.gravatar.com/avatar/e686e1911928d6ac0ca4c03b5acfd4b2a59d192775face89b9b4f6e4a08012c4?s=100&d=mm&r=g) [Mokhtar Ebrahim](https://likegeeks.com/author/admin/) Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology. ## Related posts 1. [Seaborn heatmap tutorial (Python Data Visualization)](https://likegeeks.com/seaborn-heatmap-tutorial/ "Seaborn heatmap tutorial (Python Data Visualization)") 2. [Highlight Cells & Rows in Seaborn Heatmap](https://likegeeks.com/highlight-cells-rows-seaborn-heatmap/ "Highlight Cells & Rows in Seaborn Heatmap") 3. [Customize Seaborn Heatmap x-axis and y-axis Tick Labels](https://likegeeks.com/seaborn-heatmap-axis-tick-labels/ "Customize Seaborn Heatmap x-axis and y-axis Tick Labels") 4. [Apply Masks To Seaborn Heatmap in Python](https://likegeeks.com/seaborn-heatmap-masks/ "Apply Masks To Seaborn Heatmap in Python") 5. [Customize Colorbar in Seaborn Heatmap](https://likegeeks.com/seaborn-heatmap-colorbar/ "Customize Colorbar in Seaborn Heatmap") 6. [Customize Annotation Text in Seaborn Heatmap](https://likegeeks.com/annotation-text-seaborn-heatmap/ "Customize Annotation Text in Seaborn Heatmap") ###### Leave a Reply [Cancel reply](https://likegeeks.com/nan-values-seaborn-heatmap/#respond) ##### My Published Book [![My Book](https://likegeeks.com/wp-content/uploads/2020/05/My-book.png)](https://www.packtpub.com/en-us/product/mastering-linux-shell-scripting-9781788990554) ##### Related posts 1. [Seaborn heatmap tutorial (Python Data Visualization)](https://likegeeks.com/seaborn-heatmap-tutorial/) 2. [Highlight Cells & Rows in Seaborn Heatmap](https://likegeeks.com/highlight-cells-rows-seaborn-heatmap/) 3. [Customize Seaborn Heatmap x-axis and y-axis Tick Labels](https://likegeeks.com/seaborn-heatmap-axis-tick-labels/) 4. [Apply Masks To Seaborn Heatmap in Python](https://likegeeks.com/seaborn-heatmap-masks/) 5. [Customize Colorbar in Seaborn Heatmap](https://likegeeks.com/seaborn-heatmap-colorbar/) 6. [Customize Annotation Text in Seaborn Heatmap](https://likegeeks.com/annotation-text-seaborn-heatmap/) ## Post navigation [Highlight Cells & Rows in Seaborn Heatmap](https://likegeeks.com/highlight-cells-rows-seaborn-heatmap/) [Customize Seaborn Heatmap x-axis and y-axis Tick Labels](https://likegeeks.com/seaborn-heatmap-axis-tick-labels/) - [Disclaimer](https://likegeeks.com/disclaimer/) - [Privacy Policy](https://likegeeks.com/privacy-policy/) - [About](https://likegeeks.com/about/) - [Contact](https://likegeeks.com/contact/)
Readable Markdown
One common challenge faced when creating heatmaps is the presence of NaN (Not a Number) values. These NaNs can impact the visual output, leading to misleading interpretations or an unclear representation of the data. In this tutorial, you’ll learn how to handle NaN values in [Seaborn heatmaps](https://likegeeks.com/seaborn-heatmap-tutorial/). Whether you are dealing with sparse NaNs that can be easily dropped, or require more nuanced approaches like filling, interpolating, or visually distinguishing NaNs, this tutorial covers it all. - [1 Remove NaN Values](https://likegeeks.com/nan-values-seaborn-heatmap/#Remove_NaN_Values) - [2 Fill NaNs with a Specific Value](https://likegeeks.com/nan-values-seaborn-heatmap/#Fill_NaNs_with_a_Specific_Value) - [3 Interpolate NaNs](https://likegeeks.com/nan-values-seaborn-heatmap/#Interpolate_NaNs) - [4 Mask NaNs in the Heatmap](https://likegeeks.com/nan-values-seaborn-heatmap/#Mask_NaNs_in_the_Heatmap) - [5 Use a Different Color for NaNs](https://likegeeks.com/nan-values-seaborn-heatmap/#Use_a_Different_Color_for_NaNs) ## Remove NaN Values In scenarios where NaNs are sparse and not critical to your analysis, one effective method is to remove them using the `dropna()` method. First, you’ll need to import the necessary libraries and create a sample dataset. ``` import seaborn as sns import pandas as pd import numpy as np data = { 'Call Quality': [3.5, np.nan, 4.2, 3.8], 'Internet Speed': [50, 45, np.nan, 60], 'Customer Satisfaction': [4.0, 3.6, np.nan, 4.5] } df = pd.DataFrame(data) print(df) ``` Output: ``` Call Quality Internet Speed Customer Satisfaction 0 3.5 50.0 4.0 1 NaN 45.0 3.6 2 4.2 NaN NaN 3 3.8 60.0 4.5 ``` Note the NaN values in the dataset. Next, let’s remove these NaN values: ``` cleaned_df = df.dropna() print(cleaned_df) ``` Output: ``` Call Quality Internet Speed Customer Satisfaction 0 3.5 50.0 4.0 3 3.8 60.0 4.5 ``` In the above step, `dropna()` removes rows with any NaN values. Let’s plot the heatmap using Seaborn after we’ve dropped the NaN values. This will help visualize how the removal of NaN values impacts the heatmap representation. ``` import matplotlib.pyplot as plt plt.figure(figsize=(8, 4)) sns.heatmap(cleaned_df, annot=True, cmap='viridis') plt.title('Heatmap (Without NaN Values)') plt.show() ``` Output: ![Remove NaN Values](https://likegeeks.com/wp-content/uploads/2024/02/01-Remove-NaN-Values.jpeg) ## Fill NaNs with a Specific Value In certain situations, you want to retain the original size of your dataset, especially when the presence of each row is significant for your analysis. In such cases, rather than removing NaN values, you can fill them with a specific value. The [`.fillna()` function](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.fillna.html) in Pandas allows you to do this. ``` filled_df = df.fillna(0) print(filled_df) ``` Output: ``` Call Quality Internet Speed Customer Satisfaction 0 3.5 50.0 4.0 1 0.0 45.0 3.6 2 4.2 0.0 0.0 3 3.8 60.0 4.5 ``` In the code above, `fillna(0)` replaces all NaN values in the dataset with `0`. Now, let’s create a heatmap with the NaN values filled: ``` plt.figure(figsize=(8, 4)) sns.heatmap(filled_df, annot=True, cmap='viridis') plt.title('Heatmap (NaNs Filled with 0)') plt.show() ``` Output: ![Fill NaNs with a Specific Value](https://likegeeks.com/wp-content/uploads/2024/02/02-Fill-NaNs-with-a-Specific-Value.jpeg) ## Interpolate NaNs Interpolation is another method to handle NaN values in datasets, especially when a linear relationship can be assumed between data points. By interpolating, you estimate the NaN values based on neighboring data points. This method is useful in time-series data or when the data points have a logical sequence. ``` interpolated_df = df.interpolate() print(interpolated_df) ``` Output: ``` Call Quality Internet Speed Customer Satisfaction 0 3.50 50.0 4.00 1 3.85 45.0 3.60 2 4.20 52.5 4.05 3 3.80 60.0 4.50 ``` Here, [`interpolate()` function](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.interpolate.html) calculates the NaN values by estimating them based on adjacent values. For example, in the ‘Call Quality’ column, the NaN value is replaced with an average of its neighboring values. Now, let’s visualize this interpolated data using a heatmap: ``` # Plotting the heatmap with interpolated values plt.figure(figsize=(8, 4)) sns.heatmap(interpolated_df, annot=True, cmap='viridis') plt.title('Heatmap (Interpolated NaN Values)') plt.show() ``` Output: ![Interpolate NaNs](https://likegeeks.com/wp-content/uploads/2024/02/03-Interpolate-NaNs.jpeg) ## Mask NaNs in the Heatmap Masking NaNs in a heatmap allows you to visually distinguish these values from the rest of the data. This method is useful when you want to maintain the original dataset’s structure, including the NaN values, while still providing a clear visual representation of where data is missing. ``` nan_mask = df.isna() plt.figure(figsize=(8, 4)) sns.heatmap(df, annot=True, cmap='viridis', mask=nan_mask) plt.title('Heatmap (NaNs Masked)') plt.show() ``` Output: ![Mask NaNs in the Heatmap](https://likegeeks.com/wp-content/uploads/2024/02/04-Mask-NaNs-in-the-Heatmap.jpeg) The masked areas do not have annotations and color, clearly indicating the absence of data. ## Use a Different Color for NaNs Another method to handle NaNs in heatmaps is using a different color for these values. This method highlights the NaNs distinctly and makes them easily identifiable. First, we’ll prepare a colormap that distinguishes NaNs: ``` from matplotlib.colors import ListedColormap # Custom colormap: NaNs will be shown in grey cmap = ListedColormap(sns.color_palette("viridis", as_cmap=True).colors + [(0.75, 0.75, 0.75)]) # Preparing the data: NaNs are set to a unique number unique_number_for_nans = -1 heatmap_data = df.fillna(unique_number_for_nans) plt.figure(figsize=(8, 4)) sns.heatmap(heatmap_data, annot=True, cmap=cmap, cbar=False) plt.title('Heatmap (Different Color for NaNs)') plt.show() ``` Output: ![Use a Different Color for NaNs](https://likegeeks.com/wp-content/uploads/2024/02/05-Use-a-Different-Color-for-NaNs.jpeg) ![Mokhtar Ebrahim](https://secure.gravatar.com/avatar/e686e1911928d6ac0ca4c03b5acfd4b2a59d192775face89b9b4f6e4a08012c4?s=100&d=mm&r=g) Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.
Shard88 (laksa)
Root Hash15338997981204926088
Unparsed URLcom,likegeeks!/nan-values-seaborn-heatmap/ s443