Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Improving Your Data Visualizations in Python

Cursus bekijken

Oefeninstructies

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

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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()
Code bewerken en uitvoeren