Add unique name to data for calling on another part

Hi everyone

Is it possible to have a unique name or id or … to refer to a drawn object. For example in the below code to update any line data, I have to refer them by the order of drawing them. But I need to assign a unique name (for example assign ‘L2nd’ to the second line) then I update the second line data by calling it’s unique name (‘L2nd’). What should I do for this purpose?

import plotly.graph_objects as go

fig = go.Figure()

# Create scatter trace of text labels
fig.add_trace(go.Scatter(  x=[0, 6], y=[0, 1],  mode="lines"))
fig.add_trace(go.Scatter(  x=[1, 7], y=[0, 1],  mode="lines"))
fig.add_trace(go.Scatter(  x=[2, 8], y=[0, 1],  mode="lines"))
fig.add_trace(go.Scatter(  x=[3, 9], y=[0, 1],  mode="lines"))

fig.data[0].x=[0, 8]
fig.data[1].x=[2, 5]
fig.data[2].x=[3, 4]
fig.data[3].x=[1, 4]



fig.show()

Hey @Bijan,

Please take a look at this link

https://plotly.com/python/creating-and-updating-figures/

" The update_traces() method supports a selector argument to control which traces should be updated. Only traces with properties that match the selector will be updated."

In your case:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(  x=[0, 6], y=[0, 1], name="first_line",  mode="lines"))
fig.add_trace(go.Scatter(  x=[1, 7], y=[0, 1], name="second_line",  mode="lines"))

fig.update_traces(x=[1,5], y=[1,5],selector = ({'name':'first_line'}) )

fig.show()

Have a nice day.

3 Likes