Skip to content
Advertisement

How to iterate over scatter data?

I am trying to plot a time series on a scatterplot with dropdown boxes using Plotly express. I have 2 traces filtered on some unique values in the “name” column of my data. I’m using the ‘color’ function on px.scatter to filter by this column. Since the traces have the same name, they are duplicated on the plot and I can’t tell which trace belongs to which ‘color’. I would like to change the names of each according to the color. I am trying to loop over fig.data, as I was able to store my values in a list labels (see below) but have not been successful in looping over names of the traces since it is of type scatter.

Earlier, I was able to hardcode it in the following way, however, I need it to be dynamic:

JavaScript

To make it dynamic I tried this and was able to update the values, but each of them updated to the last value in my list. I would like to update each value in the order of the list. Not sure how to do this.

JavaScript

Here is the code I used for the dropdown plot (data is a time series):

JavaScript

Advertisement

Answer

I found a solution to changing the trace names with a for loop.

Please note: df['color'] contains var1 by default. When adding var2, we need not remove var1.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement