🚀 Gen 5 of the leading AI app deployment platform launches October 6. Click for the livestream.

Add legend to the plotly express

Hi!

I’m following this tutorial: PCA Visualization | Python | Plotly
I use the following code:

pca = PCA(n_components=2)
components = pca.fit_transform(X)

colorsIdx = {'0': 'blue', '1': 'yellow'}
fig = px.scatter(components, x=0, y=1, color=df_stimuli['is_500_days'],
                labels={'0': 'PC 1', '1': 'PC 2'})
fig.update_layout(title_text=f'{brain_area}', title_x=0.5)
fig.show()

But instead of a legend with 2 classes (in my case the column df_stimuli[‘is_500_days’] contains only 0’s and 1’s) it creates a colorbar.

If I add

fig.update(layout_coloraxis_showscale=False)
fig.update(layout_showlegend=True)

It removes the colorbar, but no legend is shown.