如何解决Plotly:如何在散点图散点图中手动设置点的颜色?
https://plotly.com/python/line-and-scatter/有很多散点图示例,但没有一个示例向您展示如何在px.scatter中设置所有点的颜色:
# x and y given as DataFrame columns
import plotly.express as px
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df,x="sepal_width",y="sepal_length")
fig.show()
我尝试添加colour = 'red'
等不起作用。这些示例仅向您展示如何使用其他变量进行着色。
原则上,我可以添加另一个功能并将其设置为相同,但这似乎是完成任务的一种奇怪的方式...。
解决方法
为此,您可以使用color_discrete_sequence
参数。
fig = px.scatter(df,x="sepal_width",y="sepal_length",color_discrete_sequence=['red'])
该参数是对离散的color
因素使用自定义调色板,但是如果您对color
不使用任何因素,它将对图形中的所有点使用第一个元素。 / p>
有关离散色板的更多信息:https://plotly.com/python/discrete-color/
,据我了解您的问题,我会尽力回答。
参数“颜色”仅接受列名称。
您可以考虑使用update_traces()
import plotly.express as px
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df,y="sepal_length")
fig.update_traces(marker=dict(
color='red'))
fig.show()
参考:https://plotly.com/python/marker-style/
,您 不必添加其他功能 即可在此处获得所需的功能。感谢Python的method chaining,您只需添加.update_traces(marker=dict(color='red'))
即可将您选择的任何颜色手动分配给 all 标记。
情节:
代码:
# x and y given as DataFrame columns
import plotly.express as px
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df,y="sepal_length"
).update_traces(marker=dict(color='red'))
fig.show()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。