Adding annotations
1. Adding annotations
Let's learn how to create, style, and position annotations in Plotly visualizations.2. What are annotations?
Annotations are extra boxes of text and data added to a plot. They are always visible and serve two main purposes. Firstly, you may wish to draw attention to or add notes on a particular data point. You may also wish to add a text box with variables and text, similar to one in Microsoft Word.3. Creating annotations
Plotly offers a couple of ways to add annotations. We can add one at a time using the add_annotation() method. Or, we can use the update_layout() method with the annotations argument, which accepts a list of annotation objects. Since we've already worked with update_layout(), we'll continue using it here.4. Important annotation arguments
There are a few key arguments of an annotation object, a dictionary, worth highlighting. The text argument defines what will be shown - this can include variables. The x and y arguments specify the location of the annotation. The showarrow argument lets you include an arrow pointing to that position. And if you use an arrow, you can customize it too. Be careful of placing annotations absolutely as if your data changes, it may overlap. We'll look at how to specify positioning in both ways.5. Positioning annotations
By default, x and y refer to the data values in the plot. But we can also position annotations absolutely - meaning they're fixed on the plot rather than tied to a specific data point. To do that, we set xref and yref to "paper". This tells Plotly to interpret x and y as percentages from 0 to 1 across the plot area. For example, setting both to 0.5 places the annotation in the center.6. Data-linked annotations
Let's try this with a real example. Imagine we want to highlight our own company in a scatterplot of revenue versus employee count. We know the exact values, so we add an annotation with those x and y coordinates. We set showarrow to true, style the arrow with arrowhead, and add a short text message in black. Using update_layout(), we pass our annotation as a dictionary inside a list. This is what we produce, an arrow and some text linked to a specific point.7. Floating annotation
Now, let's create an annotation that's not tied to the data. Let's say we want to make a bold statement for investors. We have set xref and yref to "paper". Now, our x and y are not in our DataFrame units but are in a percentage across the plot to position the annotation. Let's set the position to be 50% across the x-axis and 80% up the y-axis. This time, we turned off the arrow, included our message, and gave the box a red background. The result is a styled text box that floats on the plot, no matter what the data shows. This is a great way to draw attention to key ideas or calls to action.8. Let's practice!
Let's practice creating and styling some annotations.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.