filmov
tv
Python Tutorial: Annotations

Показать описание
---
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 datapoint 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 call-outs 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 and 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 most important, 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 meant to purely 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 question: does your visualization needs to stand on its own?
If the visualization is going to need to work on its own, such as one-off charts like 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 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 a 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 this, 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 toward. 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 with our pollution data.
#PythonTutorial #Python #DataCamp #Improving #Data #Visualizations #Annotations