简体   繁体   English

如何在 Python 中更改 plotly.express 的颜色和大小?

[英]How to change color and size of plotly.express in Python?

I'm using plotly.express to make a plot of my Artificial Neural Networks in Python.我正在使用plotly.express在 Python 中绘制我的人工神经网络图。 I'll show my code:我将展示我的代码:

import plotly.express as px
fig = px.scatter(newDf, x="Glucose", y="Age", color="Outcome")
fig.show()

I would like the sidebar of "color" (comprehends the Outcome) has only 0 and 1 values (instead of 0, 0.2, 0.4...).我希望“颜色”(包含结果)的侧边栏只有 0 和 1 值(而不是 0、0.2、0.4...)。 Plus, I would like to change the color of the points in the scatter plot.另外,我想更改散点图中点的颜色。 Currently, the plot has blue(0) and yellow(1) points and, expecially the yellow ones, are difficult to see.目前,该图具有蓝色 (0) 和黄色 (1) 点,尤其是黄色点,很难看到。

Presuming your outcome only has 0 and 1 values, the easy way to make plotly treat it as a discrete value would be to convert it to a string.假设你的结果只有0和1的值,最简单的方式,使plotly把它作为一个离散值将其转换为字符串。

df["Outcome"] = df["Outcome"].astype(str) #convert to string

# you can convert back to numeric later, if needed
df["Outcome"] = df["Outcome"].astype(float) 

Now, to change the colors you can use the argument color_discrete_sequence like this:现在,要更改颜色,您可以像这样使用参数color_discrete_sequence

mport plotly.express as px
fig = px.scatter(newDf, x="Glucose", y="Age", color="Outcome", 
                 color_discrete_sequence= ["blue", "red"])
fig.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM