Need to target a specific trace by name when use update_traces() 's selector

Greeting,

According to the following documentation, I understand that we can use update_traces() 's selector to target and update a group of traces.

I need to target a specific trace by name (or index , if name is not possible) when use update_traces() 's selector.

For example, with the following trace:

fig.add_trace(go.Scatter(name='trc1', x=df.candle_date, y=df.data0_close), 1, 1)

I need to update the trace name trc1 among all other traces.

How can I do that?

Thank you,

Hey @python-trader ,

Here’s an example:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(x = [2], y = [4],  name = "first_trace",   mode = "markers", marker = dict(size = 6,  color = "red")))
fig.add_trace(go.Scatter(x = [8], y = [16], name = "second_trace",  mode = "markers", marker = dict(size = 12, color = "green")))

fig.update_traces(marker=dict(size=24, color="blue"), selector = ({'name':'first_trace'}))

fig.show()

Before Update:

After Update:

Have a nice day.

2 Likes

Hi @akroma ,

Thank you for help.

I think I am missing how to efficiently use Plotly’s documentation. I wonder how do you know about selector = ({'name':'first_trace'})? It would be a great help if I know where to find answers like this in Plotly Python documentation.

Thank you!

2 Likes

hi @python-trader
One quick way to navigate the plotly graphing library and it’s parameters is by going to the Figure reference link. Then use the top left search bar to search for key words.

In your case since you need to update the trace of a scatter plot, I searched for two key words: scatter and traces

Then I clicked on the top right result (scatter traces) which seemed to be the best match. This led me to this page:

You’re right that the answer is not always obvious, but I hope this offers you helpful plotly-graphing navigation practices

3 Likes

You can also use it like below as in the docs without curly braces.

fig.update_traces(marker=dict(size=24, color="blue"), selector = dict(name="first_trace"))

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

2 Likes

Thank you @adamschroeder , this is helpful as always. Perhaps the topic of β€œHow to navigate in Plotly documentation and find answers” could be a new video idea for your excellent YouTube channel.

1 Like