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.
Este exercício faz parte do curso
Improving Your Data Visualizations in Python
Instruções do exercício
Pass the diverging palette to your plot.
- Note that the
sns.heatmap()
function in Seaborn does not have apalette
argument but instead requires thecmap
argument.
- Note that the
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
and4
to make the legend symmetric.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# 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()