I want a trace to be highlighted (color or opacity change) when selected with mouse hover. I have looked into restyle
functionality, but it may not be appropriate for my use case.
It looks like this has been discussed on Github, but I'm not sure if it has been resolved/implemented.
Here is an example in Bokeh of what I want to accomplish in Plotly Python:
from bokeh.plotting import figure, show, output_notebook
from bokeh.models import HoverTool
from bokeh.models import ColumnDataSource
output_notebook()
p = figure(plot_width=400, plot_height=400,y_range=(0.2,0.5))
y_vals = [0.22,0.22,0.25,0.25,0.26,0.26,0.27,0.27]
y_vals2 = [y*1.4 for y in y_vals]
x_vals = [0,1,1,2,2,2,2,3]
data_dict = {'x':[x_vals,x_vals],
'y':[y_vals,y_vals2],
'color':["firebrick", "navy"],
'alpha':[0.1, 0.1]}
source = ColumnDataSource(data_dict)
p.multi_line('x','y',source=source,
color='color', alpha='alpha', line_width=4,
hover_line_alpha=1.0,hover_line_color='color')
p.add_tools(HoverTool(show_arrow=True,
line_policy='nearest',
))
show(p)
Hover Labels One of the most deceptively-powerful features of interactive visualization using Plotly is the ability for the user to reveal more information about a data point by moving their mouse cursor over the point and having a hover label appear.
Adding Traces New traces can be added to a plot_ly figure using the add_trace() method. This method accepts a plot_ly figure trace and adds it to the figure. This allows you to start with an empty figure, and add traces to it sequentially.
From the Plotly website: “A trace is just the name we give a collection of data and the specifications of which we want that data plotted. Notice that a trace will also be an object itself, and these will be named according to how you want the data displayed on the plotting surface.”
In order to keep the items visible in the legend but hidden in the plot, you need to set the values to 'legendonly'. The legend entries can then still be clicked to toggle individual visibility.
You can use Plotly's FigureWidget
functionality.
import plotly.graph_objs as go
import random
f = go.FigureWidget()
f.layout.hovermode = 'closest'
f.layout.hoverdistance = -1 #ensures no "gaps" for selecting sparse data
default_linewidth = 2
highlighted_linewidth_delta = 2
# just some traces with random data points
num_of_traces = 5
random.seed = 42
for i in range(num_of_traces):
y = [random.random() + i / 2 for _ in range(100)]
trace = go.Scatter(y=y, mode='lines', line={ 'width': default_linewidth })
f.add_trace(trace)
# our custom event handler
def update_trace(trace, points, selector):
# this list stores the points which were clicked on
# in all but one trace they are empty
if len(points.point_inds) == 0:
return
for i,_ in enumerate(f.data):
f.data[i]['line']['width'] = default_linewidth + highlighted_linewidth_delta * (i == points.trace_index)
# we need to add the on_click event to each trace separately
for i in range( len(f.data) ):
f.data[i].on_click(update_trace)
# let's show the figure
f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With