ComeçarComece de graça

Coloring ordinal categories

You are working for the Des Moines city council to assess the associations of various pollutant levels in the city. The two most important pollutants are SO2 and NO2 but CO is also of interest. You've only been allowed enough space for a single plot for your part of the report.

You start with a scatter plot of the SO2 and NO2 values as they are most important and then decide to show the CO values using a color scale corresponding to CO quartiles. By binning the continuous CO values, you have turned CO into an ordinal variable that can illuminate broad patterns without requiring much effort from the viewer to compare subtly different shades.

Este exercício faz parte do curso

Improving Your Data Visualizations in Python

Ver curso

Instruções do exercício

  • Set the qcut() function to break 'CO' into quartiles.
  • Map the color of your scatter plot to the new quartile column.
  • Change the palette to the ColorBrewer palette 'GnBu'.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Divide CO into quartiles
pollution['CO quartile'] = pd.qcut(pollution['CO'], q = ____, labels = False)

# Filter to just Des Moines
des_moines = pollution.query("city  ==  'Des Moines'")

# Color points with by quartile and use ColorBrewer palette
sns.scatterplot(x = 'SO2',
                y = 'NO2',
                ____ = '____', 
                  data = des_moines,
                palette = '____')
plt.show()
Editar e executar o código