python - Python中不同类的不同连续颜色条

标签 python plot colors plotly colorbar

我有属于三个不同类别的数据点。另一方面,我对其类别中的每个数据点都有权重。我想根据点的权重为我的点着色,但使用三种不同的连续颜色范围。实际上我想要类似下图的东西(这是手工制作的)。现在我使用 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()

最佳答案

请不要将其视为答案(暂时)。据我所知,您可以在绘图中使用不同的色标。但您应该研究如何正确显示所有图例

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

关于python - Python中不同类的不同连续颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61169893/

相关文章:

python - 当条件满足时,While 循环不会终止

Python 将 MySQL 表复制到 SQLite3

python - Matplotlib 多值条形图定制

r - ggplot2:qplot 面轴自由缩放,同时保留网格

android - 在 Android 上使用 OpenGL ES 围绕另一个球体旋转

Css 颜色设置混淆

python - 如何在 Django 应用程序中使用 celery 执行任务?

python - 交互式 Jupyter 小部件在 Jupyter Lab 中不起作用

R——使用对创建线图矩阵

java - 如何使任何 JComponent 的背景颜色闪烁?