Plotly is a powerful Python library for creating interactive plots. By default, Plotly displays information about data points when you hover over them. While this is useful in many cases, sometimes you might want to disable this hover behavior for specific traces or even the entire plot. See how to achieve this.
1. Disabling Hover Information for a Single Trace
The most common scenario is that you want to disable hover information for just one trace in your plot. You can achieve this using the hoverinfo
attribute of the trace.
import plotly.graph_objects as go
fig = go.Figure()
# Trace with hover information disabled
fig.add_trace(go.Scatter(
x=[1, 2, 3],
y=[4, 5, 6],
hoverinfo='none', # Disable hover information for this trace
name="Trace 1"
))
# Another trace with default hover information
fig.add_trace(go.Scatter(
x=[1, 2, 3],
y=[7, 8, 9],
name="Trace 2"
))
fig.show()
In this example, hoverinfo='none'
is used to disable all hover information for “Trace 1”. “Trace 2” will still have the default hover behavior.
You can also control specific aspects of the hover information using other values for the hoverinfo
attribute. For example:
'x'
: Show only x-coordinate.'y'
: Show only y-coordinate.'xy'
: Show both x and y coordinates.'text'
: Show the text specified in thetext
attribute.'name'
: Show the trace name.'all'
: Show all hover information (default).'none'
: Show no hover information.
You can combine these values using ‘+’. For example, hoverinfo='x+y+text'
will show x, y, and text information.
2. Disabling Hover Information for All Traces
If you want to disable hover information for all traces in your plot, you can use the hovermode
layout attribute.
import plotly.graph_objects as go
fig = go.Figure()
# ... add your traces ...
fig.update_layout(hovermode=False) # Disable hover information for the entire plot
fig.show()
Setting hovermode=False
in the layout will disable hover interaction for the entire figure.
3. Using the config Parameter (Less Common)
You can also control hover information through the config parameter when showing the plot, but this is less common for simple hover disabling.
import plotly.graph_objects as go
# ... add your traces ...
fig.show(config={'hovermode': False}) # Disable hover information
Which Method to Use?
- Use
hoverinfo='none'
within the trace definition to disable hover for individual traces. This is the most common and flexible approach. - Use
fig.update_layout(hovermode=False)
to disable hover information for all traces in the plot. - The config parameter is generally used for more global Plotly configurations, not just hover mode.