Python中不同类别的不同连续颜色条

问题描述 投票:1回答:1

我有属于三个不同类别的数据点。另一方面,对于类中的每个数据点,我都有权重。我想根据点的颜色为其点着色,但要使用三种不同的连续颜色范围。实际上,我想要类似下面的图像(手工制作)。现在,我正在使用Plotly进行着色,但是欢迎使用与python兼容的任何其他方法。

enter image description here

实际上我想合并代码的两个输出:

if __name__ == '__main__':
n_data = 100
n_class = 3
t1 = [random.random() for i in range(n_data)]
t2 = [random.random() for i in range(n_data)]
class_color = [str(random.randint(1,n_class)) for i in range(n_data)]
weight_color = [random.random() for i in range(n_data)]
df = pd.DataFrame()
print(len(t1))
print(len(t2))
df['x'] = t1
df['y'] = t2
df['class_color'] = class_color
df['weight_color'] = weight_color
fig1 = px.scatter(df, x="x", y="y", color="class_color")
fig1.show()

fig2 = px.scatter(df, x="x", y="y", color="weight_color")
fig2.show()
python plot colors plotly colorbar
1个回答
1
投票

[请不要以答案为准。据我所知,您可以将不同的色标配合使用。但是您应该研究如何正确显示所有图例

import plotly.graph_objects as go
import plotly.express as px
df = px.data.iris()
dfs = [d[1] for d in list(df.groupby('species'))]

fig = go.Figure()
fig.add_trace(
    go.Scatter(x=dfs[0]["sepal_width"],
               y=dfs[0]["sepal_length"],mode="markers",
               marker=dict(color=dfs[0]["sepal_length"],
                           colorscale='Viridis',
                           showscale=True),
               name=dfs[0]["species"].unique()[0],
               showlegend=False
               ))

fig.add_trace(
    go.Scatter(x=dfs[1]["sepal_width"],
               y=dfs[1]["sepal_length"],mode="markers",
               marker=dict(color=dfs[1]["sepal_length"],
                           colorscale='Magenta',
                           showscale=False),
               name=dfs[1]["species"].unique()[0],
               showlegend=False
               ))


fig.add_trace(
    go.Scatter(x=dfs[2]["sepal_width"],
               y=dfs[2]["sepal_length"],mode="markers",
               marker=dict(color=dfs[2]["sepal_length"],
                           colorscale='Cividis',
                           showscale=False),
               name=dfs[2]["species"].unique()[0],
               showlegend=False
               ))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.