简体   繁体   English

在python plotly包中的Scatter plot中操作图例

[英]Manipulating legend in Scatter plot in python plotly package

I wanted to plot a scatter plot using plotly in python with specific marker colors. 我想使用在python中使用特定标记颜色绘制散点图。 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()

在此处输入图片说明

px.scatter() actually already knows how to do this: px.scatter()实际上已经知道如何执行此操作:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM