RNs*_*ost 8 python plotly plotly-python
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()
Run Code Online (Sandbox Code Playgroud)
我试过添加colour = 'red'等不起作用。这些示例仅向您展示如何通过其他变量着色。
原则上我可以添加另一个功能并将其设置为相同,但这似乎是完成任务的一种奇怪的方式......
据我了解你的问题,我会尽力回答。
参数“颜色”仅接受列名称。
对于您的情况,您可以考虑使用 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()
Run Code Online (Sandbox Code Playgroud)
参考: https: //plotly.com/python/marker-style/
为此,您可以使用该color_discrete_sequence参数。
fig = px.scatter(df, x="sepal_width", y="sepal_length", color_discrete_sequence=['red'])
Run Code Online (Sandbox Code Playgroud)
此参数是为离散color因子使用自定义调色板,但如果您不使用任何因子,color它将使用图中所有点的第一个元素。
有关离散颜色调色板的更多信息:https ://plotly.com/python/discrete-color/
您不必添加其他功能即可在这里获得您想要的功能。感谢 Python 的方法链接,您可以手动将.update_traces(marker=dict(color='red'))您选择的任何颜色分配给所有标记。
阴谋:
代码:
# 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()
Run Code Online (Sandbox Code Playgroud)