简体   繁体   English

如何在 seaborn 散点图图例中放置颜色条

[英]How to put a colorbar in seaborn scatterplot legend

I have the next scatterplot我有下一个散点图

在此处输入图像描述

But i want to change the dots on the legend by continuos color map like this:但我想通过连续颜色 map 改变图例上的点,如下所示:

在此处输入图像描述

This is my code:这是我的代码:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

sns.set_style("whitegrid")

gene_list = pd.read_csv('interseccion.csv', header=None)
glist = gene_list.squeeze().str.strip().tolist()

names = gp.get_library_name() 

enr = gp.enrichr(gene_list= glist,
                 gene_sets=['KEGG_2019_Human'],
                 organism='Human', # don't forget to set organism to the one you desired! e.g. Yeast
                 description='KEGG',
                 # no_plot=True,
                 cutoff=0.5 # test dataset, use lower value from range(0,1)
                )

resultados = enr.results.head(15)
resultados['-log10(FDR)'] = -np.log10(resultados['Adjusted P-value'])
resultados['Genes'] = resultados['Genes'].str.split(';')
resultados['Genes'] = resultados['Genes'].apply(lambda x: len(x))

g = sns.scatterplot(data=resultados, x="-log10(FDR)", y="Term", hue='-log10(FDR)', palette="seismic"
                , size="Genes", sizes=(30, 300), legend=True)

g.legend(loc=6, bbox_to_anchor=(1, 0.5), ncol=1)
g.fig.colorbar()
plt.ylabel('')
plt.xlabel('-log10(FDR)')

When i try to put a color bar with the funcion plt.colorbar() is not possible当我尝试使用功能 plt.colorbar() 放置彩条时是不可能的

I customized the code in the official sample with the understanding that I wanted to add a legend and color bars to the Seaborn scatterplot.我自定义了官方示例中的代码,了解我想在 Seaborn 散点图中添加图例和彩条。 A colormap has been created to match the colors of the sample graph, but it can be drawn without problems by specifying the colormap name.已创建颜色图以匹配示例图的 colors,但通过指定颜色图名称可以毫无问题地绘制它。 The color bar is customized by getting its position and adjusting it manually in the legend.彩条是通过获取其 position 并在图例中手动调整来定制的。 The height of the color bar is halved to match the legend.颜色条的高度减半以匹配图例。

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")
fig, ax = plt.subplots()
g = sns.scatterplot(
    data=tips, x="total_bill", y="tip", hue="size", size="size",
    sizes=(20, 200), legend="full", ax=ax)
g.legend(loc='upper right', bbox_to_anchor=(1.2, 1.0), ncol=1)

norm = plt.Normalize(tips['size'].min(), tips['size'].max())
cmap = sns.cubehelix_palette(light=1, as_cmap=True)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])

cax = fig.add_axes([ax.get_position().x1+0.05, ax.get_position().y0, 0.06, ax.get_position().height / 2])
ax.figure.colorbar(sm, cax=cax)

plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM