Plotly:如何在 plotly express 散点图中手动设置点的颜色?
Plotly: How to manually set the color of points in plotly express scatter plots?
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'
等都不起作用。这些示例仅向您展示如何根据其他变量着色。
原则上我可以添加另一个功能并将其设置为相同,但这似乎是完成任务的一种奇怪方式....
为此,您可以使用 color_discrete_sequence
参数。
fig = px.scatter(df, x="sepal_width", y="sepal_length", color_discrete_sequence=['red'])
此参数是为离散 color
因素使用自定义调色板,但如果您没有为 color
使用任何因素,它将对图中的所有点使用第一个元素.
更多关于离散调色板的信息:https://plotly.com/python/discrete-color/
据我了解你的问题,我会尽力回答。
参数'color'只接受列名。
在你的情况下,你可以考虑使用 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()
您无需添加其他功能即可在此处获得您想要的内容。感谢 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()
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'
等都不起作用。这些示例仅向您展示如何根据其他变量着色。
原则上我可以添加另一个功能并将其设置为相同,但这似乎是完成任务的一种奇怪方式....
为此,您可以使用 color_discrete_sequence
参数。
fig = px.scatter(df, x="sepal_width", y="sepal_length", color_discrete_sequence=['red'])
此参数是为离散 color
因素使用自定义调色板,但如果您没有为 color
使用任何因素,它将对图中的所有点使用第一个元素.
更多关于离散调色板的信息:https://plotly.com/python/discrete-color/
据我了解你的问题,我会尽力回答。
参数'color'只接受列名。
在你的情况下,你可以考虑使用 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()
您无需添加其他功能即可在此处获得您想要的内容。感谢 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()