如何在Python中的plotly散点图的图例中仅显示颜色编码

发布于 2025-01-17 01:03:15 字数 1053 浏览 2 评论 0原文

我正在使用plotly.express 散点图函数绘制一些PCA,并按区域(颜色)和品种(符号)对样本进行编码。当我绘制它时,图例向我展示了所有 67 个不同品种的符号和颜色组合。有没有办法只显示颜色类别?

我的数据如下所示:

PC1PC2PC3BreedRegion
Sample1valuevaluevaluebreed1Region1Sample2valuevaluevaluebreed2Region1Sample3valuevaluevaluebreed3Region2Sample4valuevaluevaluebreed1Region1
基本什么想法
现在只是
命令代码

fig=px.scatter(pca, x="PC2",y="PC1", color="Region", symbol="Breed", labels={
    "PC2":"PC2-{}%".format(eigen[1]),
    "PC1":"PC1-{}%".format(eigen[0])
})

fig.layout.update(showlegend=True)
fig['layout']['height'] = 800
fig['layout']['width'] = 800
fig.show() 

I'm plotting some PCAs with plotly.express scatterplot function, and coding the samples by region (color) and breed (symbol). When I plot it, the legend show me all 67 different breeds in their combinations of symbols and colors. Is there a way to show only the color categories instead?

My data looks like this:

PC1PC2PC3BreedRegion
Sample1valuevaluevaluebreed1Region1
Sample2valuevaluevaluebreed2Region1
Sample3valuevaluevaluebreed3Region2
Sample4valuevaluevaluebreed1Region1

Right now my code is just the basic command:

fig=px.scatter(pca, x="PC2",y="PC1", color="Region", symbol="Breed", labels={
    "PC2":"PC2-{}%".format(eigen[1]),
    "PC1":"PC1-{}%".format(eigen[0])
})

fig.layout.update(showlegend=True)
fig['layout']['height'] = 800
fig['layout']['width'] = 800
fig.show() 

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世蒼涼 2025-01-24 01:03:15

您可以添加这些行:

region_lst = []

for trace in fig["data"]:
    trace["name"] = trace["name"].split(",")[0]
    
    if trace["name"] not in region_lst and trace["marker"]['symbol'] == 'circle':
        trace["showlegend"] = True
        region_lst.append(trace["name"])
    else:
        trace["showlegend"] = False
        
fig.update_layout(legend_title = "region")  
fig.show()

在添加代码行之前:

添加代码后:
输入图片这里的描述

我使用了这个数据框:

import plotly.express as px

df = px.data.medals_long()
fig = px.scatter(df, y="nation", x="count", color="medal", symbol="count")

You can add these lines:

region_lst = []

for trace in fig["data"]:
    trace["name"] = trace["name"].split(",")[0]
    
    if trace["name"] not in region_lst and trace["marker"]['symbol'] == 'circle':
        trace["showlegend"] = True
        region_lst.append(trace["name"])
    else:
        trace["showlegend"] = False
        
fig.update_layout(legend_title = "region")  
fig.show()

Before adding the lines of code:
enter image description here

After adding the code:
enter image description here

I used this dataframe:

import plotly.express as px

df = px.data.medals_long()
fig = px.scatter(df, y="nation", x="count", color="medal", symbol="count")
冷︶言冷语的世界 2025-01-24 01:03:15
  • 指定颜色符号会导致图例成为相应列中的值的组合
  • ,以使图例仅是一列中的值,更改为仅使用颜色
  • 要将第二列表示为符号,请更改每个跟踪以使用符号列表,这些符号
  • 已根据您的描述合成了数据

完整代码

import pandas as pd
import numpy as np
import plotly.express as px
from plotly.validators.scatter.marker import SymbolValidator

eigen = [0.5, 0.7]

# simulate data
n = 1000
pca = pd.DataFrame(
    {
        **{f"PC{c}": np.random.uniform(1, 5, n) for c in range(1, 4, 1)},
        **{
            "Breed": np.random.choice([f"breed{x}" for x in range(67)], n),
            "Region": np.random.choice([f"Region{x}" for x in range(10)], n),
        },
    }
)

# just color by Region
fig = px.scatter(
    pca,
    x="PC2",
    y="PC1",
    color="Region",
    labels={"PC2": "PC2-{}%".format(eigen[1]), "PC1": "PC1-{}%".format(eigen[0])},
)

# build dict that maps as Breed to a symbol
symbol_map = {
    t: s for t, s in zip(np.sort(pca["Breed"].unique()), SymbolValidator().values[2::3])
}

# for each trace update marker symbol to list of symbols that correspond to Breed
for t, s in zip(
    fig.data, pca.groupby("Region")["Breed"].agg(lambda x: [symbol_map[v] for v in x])
):
    t.update(marker_symbol=s)


fig

<图片src="https://i.sstatic.net/HyISk.png" alt="在此处输入图像描述">

  • specifying color and symbol results in legend being combination of values in respective columns
  • to have legend only be values in one column, change to use just color
  • to represent second column as symbols, change each trace to use a list of symbols
  • have synthesized data based on your description

full code

import pandas as pd
import numpy as np
import plotly.express as px
from plotly.validators.scatter.marker import SymbolValidator

eigen = [0.5, 0.7]

# simulate data
n = 1000
pca = pd.DataFrame(
    {
        **{f"PC{c}": np.random.uniform(1, 5, n) for c in range(1, 4, 1)},
        **{
            "Breed": np.random.choice([f"breed{x}" for x in range(67)], n),
            "Region": np.random.choice([f"Region{x}" for x in range(10)], n),
        },
    }
)

# just color by Region
fig = px.scatter(
    pca,
    x="PC2",
    y="PC1",
    color="Region",
    labels={"PC2": "PC2-{}%".format(eigen[1]), "PC1": "PC1-{}%".format(eigen[0])},
)

# build dict that maps as Breed to a symbol
symbol_map = {
    t: s for t, s in zip(np.sort(pca["Breed"].unique()), SymbolValidator().values[2::3])
}

# for each trace update marker symbol to list of symbols that correspond to Breed
for t, s in zip(
    fig.data, pca.groupby("Region")["Breed"].agg(lambda x: [symbol_map[v] for v in x])
):
    t.update(marker_symbol=s)


fig

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文