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'
等不起作用。这些示例仅向您展示如何使用其他变量进行着色。
原则上,我可以添加另一个功能并将其设置为相同,但这似乎是完成任务的一种奇怪的方式...。
答案 0 :(得分:5)
为此,您可以使用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/
答案 1 :(得分:2)
据我了解您的问题,我会尽力回答。
参数“颜色”仅接受列名称。
您可以考虑使用update_traces()
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.update_traces(marker=dict(
color='red'))
fig.show()
答案 2 :(得分:1)
您 不必添加其他功能 即可在此处获得所需的功能。感谢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,x="sepal_width",
y="sepal_length"
).update_traces(marker=dict(color='red'))
fig.show()