简体   繁体   English

Plotly:如何手动设置 plotly express 散点图中点的颜色?

[英]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.

相关问题 Plotly:如何使用 Plotly Express 组合散点图和线图? - Plotly: How to combine scatter and line plots using Plotly Express? Plotly:如何在不使用 plotly express 时为散点图设置标题颜色? - Plotly: How to set title color for scatter traces when not using plotly express? 如何创建 3d 散点图以在 plotly 中进行聚类,并将每个点颜色设置为我们选择的颜色? - How to create 3d scatter plots for clustering in plotly with having each point color set to color of our choice? Plotly 带有图形对象的散点图的颜色列表 - Plotly Color list for Scatter Plots with Graph Objects Plotly:如何为散点图中的每个系列设置唯一的颜色? - Plotly: How to set a unique color for each series in a scatter plot? 单击 Plotly 散点图中的数据点时打开 URL [Python] - Open URL when clicking on data points in Plotly scatter plots [Python] Plotly Express 散点图不绘制完整范围的 x 变量 - Plotly Express Scatter plots does not plot full range of x variables 如何为 python plotly 开放街道 map 手动设置颜色图例 - How to set color legend manually for python plotly open street map 如何在 plotly (python) 中根据我的数据集中的“颜色”列更改散点图点的颜色? - How can I change the color of my scatter plot points based on a "color" column in my dataset in plotly (python)? 如何手动为 plotly 散点图中的每个点着色? - How can I manually color each point in my scatter-plot in plotly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM