Customizing hover information and legends
1. Customizing hover information and legends
Let's learn to customize hover information and legends.2. What do we mean by hover?
Hover information appears when your mouse moves over a data point in a Plotly visualization. Out of the box, some hover details are shown automatically.3. Other default hover information
The default hover mode is "closest," which we've seen so far. But we can change this. Setting it to "x" or "y" adds a highlight along the respective axis. Choosing "x unified" or "y unified" displays a line and a hover box showing all values along that axis.4. Hover information using plotly.express
There are several key arguments for customizing hovers with plotly express. The hover_name argument is a specified column that will appear in bold at the top of the hover box. The hover_data argument accepts either a list of columns or a dictionary that includes or excludes specific columns. For example, if we set a column in the dictionary to False, it won't appear in the hover.5. Variables in hover information
We can also display columns that aren't in the visualization itself. In this scatterplot of revenue versus company size, we'll add the company's age using the hover_data argument. Even though age isn't plotted, it still appears in the hover and the company name.6. Styling hover information
There are two main ways to style hover content. One uses the hoverlabel layout element, which accepts a dictionary of style properties, such as font, background color, and alignment. The second option is hovertemplate, an HTML-like string for formatting hover text. This one is more advanced and beyond the scope of this course.7. What is a legend?
Another customization we might want is a legend - a visual guide that helps explain elements in your plot, like color or symbol. With Plotly, legends are usually added automatically when needed. For example, when we used the color argument in the student scores bar chart, the legend appeared without us having to ask for it.8. Creating and styling the legend
To control the legend, we use update_layout. We can set showlegend to True to display it. Then, we can style it using the legend argument, which takes a dictionary of properties. For example, x and y let us position the legend within the plot area. These values range from 0 to 1 and represent percentages along the axes. We can also customize the background color, border width, and font. As always, check the documentation for more!9. A styled legend
Now, let's apply this to our finance scatterplot. First, we use update_layout and set showlegend to be True. We set legend_title_text to "All Companies" to give our legend a descriptive heading. Inside the legend dictionary, we use the x and y arguments to position it in the upper-right quadrant of our plot. These arguments range from 0 to 1 and set how far along the x and y axes it will be positioned. We also make the background color a faded yellow. Here we have it, a deliberately positioned, titled, yellow background legend.10. Let's practice!
Let's practice customizing hover information and legends in our plots.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.