Introduction to Data Visualization with Plotly in Python
Alex Scriven
Data Scientist
Hover information: The text and data that appears when your mouse hovers over a data point in a Plotly visualization.
By default, you get some hover information already:
The relevant layout argument is hovermode
(defaults to closest
), which can be set to different values:
x
or y
: adds a highlight on the x or y axisx unified
/ y unified
: A dotted line appears on the relevant axis (x
here) and the hover-box is formatted
Customizing hover data in plotly.express
:
hover_name
= A specified column that will appear in bold at the top of the hover boxhover_data
= A list of columns to include or a dictionary to include/exclude columns{column_name: False}
(this will exclude column_name
)
No extensive formatting options
Hover columns don't need to be in the plot!
fig = px.scatter(revenues,
x="Revenue",
y="employees",
hover_data=['age'])
fig.show()
We can see age
in the hover!
There are two main ways to style hover information:
hoverlabel
layout elementhovertemplate
layout elementA legend is an information box that provides a key to the elements inside the plot, particularly the color or style.
You can turn on and style the legend using update_layout()
showlegend
= True
shows the default legendlegend
= a dictionary specifying styles and positioning of the legendx
, y
: (0-1) the percentage across x or y axis to positionbgcolor
(background color), borderwidth
, title
, and font
As always - check the documentation (link) for more!
We can create a styled legend and position it:
fig.update_layout({ 'showlegend': True,
'legend': { 'title': 'All Companies', 'x': 0.5, 'y': 0.8,
'bgcolor': 'rgb(246,228,129)'} })
Introduction to Data Visualization with Plotly in Python