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

Add legend to the plotly express


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)

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


It removes the colorbar, but no legend is shown.