In data visualization, it’s important not to assume that the data speaks for itself. Occasionally, the conclusion of a graph is blatantly obvious at first glance. The majority of the time, however, even if the data visualization reveals valuable information, providing some guidance on how to read it is very helpful.

Let’s do just that, and improve the histogram from our last exercise by adding AB lines and annotations. Both are added to matplotlib graphs using general functions.

An AB line is a straight line added to a graph (i.e. “from point A to point B”). It can be vertical, horizontal, or diagonal. Generally, an AB line helps to demarcate one area from another or provide context that helps the viewer make sense of the data in the graph. In matplotlib, the function plt.axvline() makes a vertical AB line, and plt.axhline() makes a horizontal AB line. We’ll work with plt.axvline(), which takes the following parameters:

  • x: where to position the line along the x-axis
  • ymin: how close to the bottom of the graph the line starts. Setting ymin=0 starts the line at the bottom of the graph, ymin=.5 would start it halfway up, and ymin=1 would start at the top of the graph. Usually set to 0
  • ymax: how close to the bottom of the graph the line ends. Setting ymax=0 ends the line at the bottom of the graph, ymax=.5 would end it halfway up, and ymax=1 would end at the top of the graph. Usually set to 1
  • linewidth: line width of the AB line
  • dashes: dash pattern given as (line_length, space_length)
  • color: color of the AB line

plt.axhline() functions exactly the same, but with y, xmin, and xmax parameters to determine position.

For example, the following code could be used to make a horizontal AB line for “mastery” on a bar graph of test scores out of 100.

plt.bar( x = school_subject, y = test_score) plt.axhline(y = 85, xmin = 0, xmax = 1, linewidth = 2, color = red)

We can easily add annotations to a graph (or label an AB line) using plt.annotate(), which allows us to position and format text on a graph. To split an annotation over two or more lines, we can simply add line breaks to the annotation text using \n.

plt.annotate() takes the following arguments:

  • text: annotation text
  • xy: (x, y) coordinate position for annotation
  • color: color of annotation text and arrow



Run the Setup cells above this cell. Then, run the code below to load the histogram we made in the last exercise. Consider: where should we place the AB line(s)?


Let’s mark the minimum and maximum legal catch sizes for lobsters: 83mm minimum, and 127mm maximum. Place one vertical AB line at each of these numbers. Set the linewidth to 2, dashes equal to (1,2), and color to mediumblue.


Nice! Now we can see clearly that the first peak in the population distribution happens just before lobsters become too big to be legally caught. Interesting! Let’s annotate these lines to make that connection more obvious. Add an annotation for ‘Minimum legal catch size’ to the line at x = 84mm and y = 23 and ‘Maximum legal catch size’ at x = 128mm and y = 7.


So close! Let’s fix up the spacing on the second annotation to keep the graph tidy. Split the ‘Maximum legal catch size’ annotation into two lines using a line break, \n, instead of the space between the words ‘legal’ and ‘catch’.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?