IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Improving Your Data Visualizations in Python

Visualizza il corso

Istruzioni dell'esercizio

  • 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'.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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()
Modifica ed esegui il codice