plotly making marker size bigger at random locations

I’m plotting a graph for the dataframe – link the code looks like –

client = ['av']
x_axis = 'recall_at_k'
y_axis = 'precision_at_k'
color_group='facilityname'
graph_width = 1200
graph_height = 600
marker_size = 7
cutoff_marker_size = 18
symbol_group='rank_cutoff'
text = 'info'

title="test figure"

fig = px.scatter(
    plot_df, 
    x=x_axis, 
    y=y_axis,
    color=color_group,
    title=title,
    width=graph_width, 
    height=graph_height,
    text=text
)
fig.update_traces(marker={'size': np.where(plot_df['is_cutoff']==1, cutoff_marker_size, marker_size), 'line':{'width':2, 'color':'DarkSlateGrey'}},textfont_size=1)

fig.show()

Here, the ‘is_cutoff’ column is a boolean column and I want to make the marker size bigger when is_cutoff’s value is ‘1’.

The problem I’m facing is that plotly is making the marker size bigger at places where ‘is_cutoff’ value is ‘0’.

to test the graph – just look at the graphs for ‘cala’ and ‘each’. image link

Answer

Each color group, i.e. facility name in your example, is a trace. With update_traces() you change the marker size of each trace in the same way. For example, fig.update_traces(marker={'size': [18, 10, 10]}) will set for each color group the size of the first marker to 18. This is not what you want because you want to modify each trace in a different way. This can be achieved with for_each_trace().

So replace the line with fig.update_traces() by

fig.for_each_trace(
    lambda trace: trace.update(
        marker={
            'size': np.where(plot_df[plot_df.facilityname==trace.name]['is_cutoff']==1, cutoff_marker_size, marker_size),
            'line':{'width':2, 'color':'DarkSlateGrey'}
        },
        textfont_size=1
   )
)

Basically, this code loops through each trace and for each trace, the dataframe is reduced to the corresponding rows with plot_df[plot_df.facilityname==trace.name]. Everything else is identical to your solution.