在绘图中对线进行分组

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

enter image description here我使用 Plotly 制作了下图,但是,我的每个样本都有一种颜色。我希望具有相同首字母的样品具有相同的颜色。例如:所有带有前缀 SA 的颜色都相同。

fig2 = px.scatter()

fig2.add_trace(go.Scatter(x=x1, y=y1,
                    mode='lines',
                    name='Mantle Growth Curve', ))
fig2.add_trace(go.Scatter(x=x2, y=y2,
                    mode='lines',
                    name='Bulk Earth'))
fig2.add_trace(go.Scatter(x=x3, y=y3,
                    mode='lines',
                    name='Continental crust Rb/Sr = 0.17'))

for i, age_value in enumerate(age):
    # Adicionar traço (trace) ao gráfico
    a = [sri[i], srm[i]]
    b = [-(age_value), 0]
    fig2.add_trace(go.Scatter(x=b, y=a, mode='lines', name=samples[i]))


fig2.update_layout(
    xaxis=dict(
        title='Age (Ga)',
        range=[-4.5, 0]  # Definindo os valores mínimo e máximo do eixo X
    ),
    yaxis=dict(
        title='87Sr/86Sr',
        range=[0.65, 0.9]  # Definindo os valores mínimo e máximo do eixo Y
    )
)

fig2.update_layout(
    template=large_rockwell_template# Definindo a cor de fundo do gráfico
)


fig2.show()

我希望这些行能够按相同前缀的相同颜色进行分组

python jupyter-notebook graphics plotly scatter-plot
1个回答
0
投票

您可能需要一种根据样本前缀定义颜色的方法。您可以获取前缀的哈希值并根据它获取颜色索引,如下所示

import plotly.express as px

colors = px.colors.qualitative.Set1

for i, age_value in enumerate(age):
    prefix = samples.split("-")[0]
    color_index = hash(prefix) % len(colors)
    a = [sri[i], srm[i]]
    b = [-(age_value), 0]
    fig2.add_trace(go.Scatter(x=b, y=a, mode='lines', name=samples[i], line=dict(color=colors[color_index]))
© www.soinside.com 2019 - 2024. All rights reserved.