[英]Plotly: How to manually set the color of points in plotly express scatter plots?
https://plotly.com/python/line-and-scatter/ has many scatter plot examples, but not a single one showing you how to set all the points' colours within px.scatter: https://plotly.com/python/line-and-scatter/有许多分散 plot 示例,但没有一个向您展示如何在 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()
I've tried adding colour = 'red'
etc doesn't work.我试过添加
colour = 'red'
等不起作用。 These examples only show you how to colour by some other variable.这些示例仅向您展示如何通过其他变量着色。
In principle I could add another feature and set it all the same but that seems a bizzare way of accomplishing the task....原则上,我可以添加另一个功能并将其设置为相同,但这似乎是完成任务的一种奇怪的方式......
For that you may use the color_discrete_sequence
argument.为此,您可以使用
color_discrete_sequence
参数。
fig = px.scatter(df, x="sepal_width", y="sepal_length", color_discrete_sequence=['red'])
This argument is to use a custom color paletter for discrete color
factors, but if you are not using any factor for color
it will use the first element for all the points in the plot.此参数是为离散
color
因子使用自定义调色板,但如果您不使用任何color
因子,它将使用 plot 中所有点的第一个元素。
More about discrete color palletes: https://plotly.com/python/discrete-color/有关离散调色板的更多信息: https://plotly.com/python/discrete-color/
As far as I understand your question, I would try to answer it.据我了解你的问题,我会尽力回答。
The parameter 'color' only accepts the column names.参数 'color' 只接受列名。
In your case, you can consider using update_traces()在您的情况下,您可以考虑使用 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()
Reference: https://plotly.com/python/marker-style/参考: https://plotly.com/python/marker-style/
You do not have to add another feature to get what you want here.您无需添加其他功能即可在此处获得所需内容。 Thanks to Python's method chaining , you can just include
.update_traces(marker=dict(color='red'))
to manually assign any color of your choosing to all markers.多亏了 Python 的方法链,您可以只包含
.update_traces(marker=dict(color='red'))
以手动将您选择的任何颜色分配给所有标记。
Plot: Plot:
Code:代码:
# 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.