Plotly: How to manually set the color of points in plotly express scatter plots?(Plotly:如何手动设置 plotly express 散点图中点的颜色?)
问题描述
https://plotly.com/python/line-and-scatter/ 有许多散点图示例,但没有一个向您展示如何在 px.scatter 中设置所有点的颜色:
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:
# 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'
等不起作用.这些示例仅向您展示如何通过其他变量进行着色.
I've tried adding colour = 'red'
etc doesn't work. 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....
推荐答案
为此,您可以使用 color_discrete_sequence
参数.
For that you may use the color_discrete_sequence
argument.
fig = px.scatter(df, x="sepal_width", y="sepal_length", color_discrete_sequence=['red'])
这个参数是为离散的 color
因素使用自定义调色板,但如果你没有为 color
使用任何因素,它将使用第一个元素情节中的点.
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.
关于离散调色板的更多信息:https://plotly.com/python/discrete-color/
More about discrete color palletes: https://plotly.com/python/discrete-color/
这篇关于Plotly:如何手动设置 plotly express 散点图中点的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Plotly:如何手动设置 plotly express 散点图中点的颜色?
基础教程推荐
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01