In this segment, the instructor advocates for the strategic use of text annotations in matplotlib visualizations, emphasizing their compactness and ability to convey information directly at the point of interest, thereby reducing reliance on legends and keeping viewers' focus. The talk outlines a decision framework: use annotations when the plot is meant to convey a clear message or stand alone (e.g., reports, social media graphics), but avoid them for exploratory plots used by experts where they may bias interpretation or add clutter. Practical guidance follows, demonstrating the simplest form of annotation using the `text()` function (specifying x, y, text, and alignment) and the more flexible `annotate()` function, which adds an arrow to point from a sparsely placed label to a crowded data point. The speaker stresses brevity, relevance, and the trade‑off between the time required to create annotations and the value they add.
Chapters
Text annotations are another fantastic and unfortunately underutilized option for calling attention to specific parts of your visualization. A major plus of annotations is their compactness. Instead of changing a color and adding a legend, you can simply place the information about the data point right by it. This will help you get the desired information across to the viewer in less ink and also keeps the user's eyes fixed near the point instead of requiring them to scan to the legend. Another benefit of annotations beyond simple callouts, is to inject deeper information or analysis into the visualization that may otherwise not be obvious to the reader. This could be some insight on patterns observed in this plot or historical significance of some value. Try to keep them brief though, so people don't get bored. While annotations almost always add something to a visualization, they can be time consuming to make, so deciding when they are worth the effort is valuable. A good rule of thumb is to think about two things. First and foremost, is the visualization going to be shown to people to convey a message? For instance, you have compiled a report to show to colleagues on a subset of data you are most familiar with. If yes, you should use annotations. Annotations will help your viewers orient with the data and kick off their exploration of the chart. If the plot is purely meant to show the data for exploration by experts, then an annotation can just get in the way and worse, even bias the exploration process. You then need to ask yourself the second does your visualization need to stand on its own? If the visualization is going to need to work on its own, such as one off charts like a tweeted photo, annotations can help you avoid surplus text on the margins or legends of the figure that people tend to ignore. If your plot isn't trying to convey a message and doesn't need to stand on its own, such as a chart taking place in a longer report, adding annotations can actually just serve to clutter up the plot, as the required information for deciphering colors or shapes is known from the context and you should keep it simple. The most simple annotation you can add to your visualization is a thoughtfully placed block of text. You you can do this with the text method in matplotlib. Just supply the x and Y locations of the text, the text body, and the text alignment. This draws a simple text annotation on the plot. Simple text annotations are great for points that are unambiguous, but what if you want to draw attention to a point in a rather crowded part of the plot. In this instance, placing a simple annotation right next to the point may obscure surrounding points, or it may be hard to tell which point is referenced. Luckily, matplotlib has a simple solution to the annotate function. With the annotate function, you supply not only an x and y location for the text, but also x and y coordinates for an arrow to point towards. Using this method, you can place your text in a sparsely populated portion of the plot and direct the user's attention to the correct point. Enough about the benefits of annotations, let's actually make some.