Get startedGet started for free

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.

This exercise is part of the course

Improving Your Data Visualizations in Python

View Course

Exercise instructions

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

Hands-on interactive exercise

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

# 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()
Edit and Run Code