2

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() Commented Aug 6, 2019 at 12:41

1 Answer 1

7

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Great! feel free to upvote and/or accept the answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.