如何在px.scatter()图中将标记和标记边框分配相同的颜色?

huangapple go评论87阅读模式
英文:

how to assign same color to marker and marker border in px.scatter() plot?

问题

抱歉,我无法执行您的请求,因为您要求我不回答翻译问题。如果您有其他需要,请随时提出。

英文:

Hi I am using below code from official website.

https://plotly.com/python/marker-style/

The marker border color is defined as same color which is 'DarkSlateGrey'. I am wondering if it is possible to make border color the same as marker color for different markers?
Thanks

import plotly.express as px

df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",color_discrete_sequence=['blue','red','green'])

fig.update_traces(marker=dict(size=12,
                              line=dict(width=2,
                                        color='DarkSlateGrey')),
                  selector=dict(mode='markers'))
fig.show()

答案1

得分: 1

有可能有另一种方法来完成这个任务,但你是否想要为图形对象中的每个类别指定一种颜色并进行循环处理呢?或者,你可以直接重写用于创建图形的数据。我的建议是直接更新数据。

import plotly.express as px

df = px.data.iris()
fig = px.scatter(df, 
                 x="sepal_width",
                 y="sepal_length",
                 color="species",
                 color_discrete_sequence=['blue','red','green'])

fig.update_traces(marker=dict(size=12,
                              line=dict(
                                  width=2,
                                  color='DarkSlateGrey'
                              )),
                  selector=dict(mode='markers'))

for i,c in enumerate(['blue','red','green']):
    fig.data[i].marker.line.color=c

fig.show()

如何在px.scatter()图中将标记和标记边框分配相同的颜色?

英文:

There may be another way to do it, but do you want to specify a color for each category in the graph object and loop through it? Or, you could directly rewrite the data used to create the graph. My answer is to update the data directly.

import plotly.express as px

df = px.data.iris()
fig = px.scatter(df, 
                 x="sepal_width",
                 y="sepal_length",
                 color="species",
                 color_discrete_sequence=['blue','red','green'])

fig.update_traces(marker=dict(size=12,
                              line=dict(
                                  width=2,
                                  color='DarkSlateGrey'
                              )),
                  selector=dict(mode='markers'))

for i,c in enumerate(['blue','red','green']):
    fig.data[i].marker.line.color=c
    
fig.show()

如何在px.scatter()图中将标记和标记边框分配相同的颜色?

huangapple
  • 本文由 发表于 2023年7月28日 06:21:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定