Get startedGet started for free

Arrow annotations

Imagine you are a city planner for Long Beach, California. Long Beach is located on the Pacific Ocean and has a large firework show every New Year's Eve. You want to look into whether this show negatively impacts the air quality of the city. To do this, you will look at CO and NO2 levels on New Year's Day. However, it turns out that New Year's Day is not one of the outliers in the plot on the right, it's located in one of the more crowded areas.

To help guide the reader to this point, you'll use an annotation along with an arrow that points to the New Year's Day value. This will provide a nice annotation that explains what the viewer is looking while printing the text in a less crowded region of the plot.

This exercise is part of the course

Improving Your Data Visualizations in Python

View Course

Exercise instructions

  • Grab the row from jan_pollution that corresponds to New Year's Day 2012 in the city of Long Beach using the pandas' .query() method.
  • Set the endpoint of the arrow (xy) by using the CO and NO2 column values from the lb_newyears DataFrame.
  • Use the argument xytext to place the annotation arrow's text in the bottom left corner of the display at x = 2, y = 15.
  • 'shrink' the arrow to 0.03, so it doesn't occlude the point of interest.

Hands-on interactive exercise

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

# Query and filter to New Years in Long Beach
jan_pollution = pollution.query("(month  ==  1) & (year  ==  2012)")
lb_newyears = jan_pollution.query("(____  ==  ____) & (____  ==  '____')")

sns.scatterplot(x = 'CO', y = 'NO2',
                data = jan_pollution)

# Point arrow to lb_newyears & place text in lower left 
plt.annotate('Long Beach New Years',
             xy = (____, ____),
             xytext = (____, _____), 
             # Shrink the arrow to avoid occlusion
             arrowprops = {'facecolor':'gray', 'width': 3, '____': ____},
             backgroundcolor = 'white')
plt.show()
Edit and Run Code