CommencerCommencer gratuitement

Cleaning up bands for overlaps

You are working for the city of Denver, Colorado and want to run an ad campaign about how much cleaner Denver's air is than Long Beach, California's air. To investigate this claim, you will compare the SO2 levels of both cities for the year 2014 (provided as the DataFrame SO2_compare). Since you are solely interested in how the cities compare, you want to keep the bands on the same plot. To make the bands easier to compare, decrease the opacity of the confidence bands and set a clear legend.

Cet exercice fait partie du cours

Improving Your Data Visualizations in Python

Afficher le cours

Instructions

  • Filter the SO2_compare to the for loop's currently selected city.
  • Color both the intervals and mean lines with the color accompanying each city.
  • Lower the interval and mean line opacities to 0.4 and 0.25, respectively.
  • Override the default legend labels in plt.plot() by setting the label argument to the city name.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

for city, color in [('Denver',"#66c2a5"), ('Long Beach', "#fc8d62")]:
    # Filter data to desired city
    city_data = SO2_compare[SO2_compare.____  ==  ____]

    # Set city interval color to desired and lower opacity
    plt.fill_between(x = 'day', y1 = 'lower', y2 = 'upper', data = city_data,
                     color = ____, alpha = ____)
    
    # Draw a faint mean line for reference and give a label for legend
    plt.plot('day','mean', data = city_data, ____ = city,
             color = ____, alpha = ____)

plt.legend()
plt.show()
Modifier et exécuter le code