1
votes

I wanted to plot a scatter plot using plotly in python with specific marker colors. But when I was successful in doing that the legend did not correspond. All the labels are now of same color(blue) in legend. I want legend to represent the risks from my data with same colors as mentioned in code. Like this:

blue marker=moderately low
green marker=moderate
orange marker=moderately high

import plotly.express as px
import plotly.graph_objects as go

colorsIdx = {'Moderately Low': 'blue', 'Moderate': 'green', 'Moderately High': 'orange'}
cols = data['Risk'].map(colorsIdx)

fig = px.scatter(data, x="1_Yr_Return", y="Expense_Ratio", color='Risk')

fig.update_traces(marker=dict(size=12, color=cols), selector=dict(mode='markers'))
fig.show()

enter image description here

1
pass this paramter color_discrete_map = colorsIdx inside px.scatter()vb_rises

1 Answers

3
votes

px.scatter() actually already knows how to do this:

import plotly.express as px

colorsIdx = {'Moderately Low': 'blue', 'Moderate': 'green', 'Moderately High': 'orange'}

fig = px.scatter(data, x="1_Yr_Return", y="Expense_Ratio", 
                 color='Risk', color_discrete_map=colorsIdx)

fig.show()