Get startedGet started for free

Customizing a diverging palette heatmap

The default color scheme used by Seaborn's heatmap() doesn't give the value of 0 any special treatment. This is fine for instances when 0 isn't special for the variable you're visualizing but means you will need to customize the palette when 0 is special, such as when it represents a neutral value.

For this visualization, you want to compare all the cities against the average pollution value for CO in November 2015. (As is provided in the DataFrame nov_2015_CO).

To do this, use a heat map to encode the number of standard deviations away from the average each city's CO pollution was for the day. You'll need to replace the default palette by creating your own custom diverging palette and passing it to the heatmap and informing the function what your neutral value is.

This exercise is part of the course

Improving Your Data Visualizations in Python

View Course

Exercise instructions

  • Pass the diverging palette to your plot.

    • Note that the sns.heatmap() function in Seaborn does not have a palette argument but instead requires the cmap argument.
  • Add your neutral value to the heat map by specifying the center argument.

  • Set the upper and lower boundaries to the color bar to -4 and 4 to make the legend symmetric.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Define a custom palette
color_palette = sns.diverging_palette(250, 0, as_cmap = True)

# Pass palette to plot and set axis ranges
sns.heatmap(nov_2015_CO,
            ____ = ____,
            ____ = ____,
            vmin = ____,
            vmax = ____)
plt.yticks(rotation = 0)
plt.show()
Edit and Run Code