如何在Python中的plotly散点图的图例中仅显示颜色编码
我正在使用plotly.express 散点图函数绘制一些PCA,并按区域(颜色)和品种(符号)对样本进行编码。当我绘制它时,图例向我展示了所有 67 个不同品种的符号和颜色组合。有没有办法只显示颜色类别?
我的数据如下所示:
PC1 | PC2 | PC3 | Breed | Region | |
---|---|---|---|---|---|
Sample1 | value | value | valuebreed1Region1Sample2valuevaluevaluebreed2Region1Sample3valuevaluevaluebreed3Region2Sample4valuevaluevaluebreed1Region1 | | |
基本 | | | | 什么 | 想法 |
的 | ? | 现在 | 吗 | 我 | 只是 |
: | 有 | 命令 | 代码 | | |
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:
PC1 | PC2 | PC3 | Breed | Region | |
---|---|---|---|---|---|
Sample1 | value | value | value | breed1 | Region1 |
Sample2 | value | value | value | breed2 | Region1 |
Sample3 | value | value | value | breed3 | Region2 |
Sample4 | value | value | value | breed1 | Region1 |
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以添加这些行:
在添加代码行之前:
添加代码后:

我使用了这个数据框:
You can add these lines:
Before adding the lines of code:

After adding the code:

I used this dataframe:
完整代码
<图片src="https://i.sstatic.net/HyISk.png" alt="在此处输入图像描述">
full code