使用Matplotlib在带有图例的热图中显示不同大小的圆圈 [英] Showing different size circles in heatmap with legend using Matplotlib

查看:115
本文介绍了使用Matplotlib在带有图例的热图中显示不同大小的圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要问的是源于此原始帖子的问题

任何帮助将不胜感激!

解决方案

问题在于复制的代码填充了所有字段,而您的代码不一定在每个框中都有一个条目.我们必须向上看,必须在其中绘制每个圆:

 将numpy导入为np导入matplotlib.pyplot作为plt从matplotlib.collections导入PatchCollection将熊猫作为pd导入x = {'ID':{0:'GO:0002474',1:'GO:0052548',2:"GO:0002483",3:'GO:0043062',4:"GO:0060333"},'TERM':{0:'通过MHC I类进行抗原加工和肽抗原的呈递',1:内肽酶活性的调节",2:内源肽抗原的抗原加工和呈递",3:细胞外结构组织",4:γ干扰素介导的信号传导途径"},'Count':{0:11,1:17,2:5,3:15,4:6},'比率':{0:18.64,1:14.53,2:8.47,3:12.82,4:10.17},'pvalue':{0:-15.83,1:-11.39,2:-9.67,3:-9.05,4:-7.41},'qvalue':{0:-11.63,1:-7.49,2:-6.52,3:-5.63,4:-4.55},'标签':{0:'NODAL',1:'GFP',2:'NODAL',3:'SHARED',4:'NODAL'}}A2780_GOBP = pd.DataFrame(x)cmap =血浆".#检索唯一标签ylabels = A2780_GOBP ["TERM"].unique().tolist()xlabels = A2780_GOBP ["Label"].unique().tolist()xn = len(xlabels)yn = len(ylabels)#检索尺寸和颜色信息s = A2780_GOBP [计数"].值c = A2780_GOBP ["pvalue"].values#准备图形及其网格无花果,ax = plt.subplots(figsize =(10,5))ax.set_xlim(-0.5,xn-0.5)ax.set_ylim(-0.5,yn-0.5)ax.set(xticks = np.arange(xn),yticks = np.arange(yn),xticklabels = xlabels,yticklabels = ylabels)ax.set_xticks(np.arange(xn)-0.5,minor = True)ax.set_yticks(np.arange(yn)-0.5,minor = True)ax.grid(which ='minor')#确保圈子显示为圈子ax.set_aspect(等于",框")#创建圈子补丁和颜色条R = s/s.max()/2circle = i的圆= [plt.Circle((xlabels.index(A2780_GOBP.loc [i,"Label"]),ylabels.index(A2780_GOBP.loc [i,"TERM"])),radius = r),枚举中的r]col = PatchCollection(圆圈,array = c,cmap = cmap)ax.add_collection(col)fig.colorbar(col)plt.show() 

示例输出:

该代码不会检查原始数据库的完整性,即每个标签-术语"对确实只出现一次.

I am asking a question stemming from this original post Heatmap with circles indicating size of population

I am trying to replicate this using my dataframe, however, my circles are non aligning to the plot. Secondary, I want to also create a legend which indicates the value relative to the size of circle.

   x= {'ID': {0: 'GO:0002474',
      1: 'GO:0052548',
      2: 'GO:0002483',
      3: 'GO:0043062',
      4: 'GO:0060333'},
     'TERM': {0: 'antigen processing and presentation of peptide antigen via MHC class I',
      1: 'regulation of endopeptidase activity',
      2: 'antigen processing and presentation of endogenous peptide antigen',
      3: 'extracellular structure organization',
      4: 'interferon-gamma-mediated signaling pathway'},
     'Count': {0: 11, 1: 17, 2: 5, 3: 15, 4: 6},
     'Ratio': {0: 18.64, 1: 14.53, 2: 8.47, 3: 12.82, 4: 10.17},
     'pvalue': {0: -15.83, 1: -11.39, 2: -9.67, 3: -9.05, 4: -7.41},
     'qvalue': {0: -11.63, 1: -7.49, 2: -6.52, 3: -5.63, 4: -4.55},
     'Label': {0: 'NODAL', 1: 'NODAL', 2: 'NODAL', 3: 'SHARED', 4: 'NODAL'}}

A2780_GOBP= pd.DataFrame(x)

Attempted Code:

ylabels = A2780_GOBP["TERM"]
xlabels = ["GFP","SHARED","NODAL"]
x, y = np.meshgrid(np.arange(len(xlabels)), np.arange(len(ylabels)))
s = A2780_GOBP["Count"].values
c = A2780_GOBP["pvalue"].values

fig, ax = plt.subplots()

R = s/s.max()/2
circles = [plt.Circle((j,i), radius=r) for r, j, i in zip(R.flat, x.flat, y.flat)]
col = PatchCollection(circles, array=c.flatten(), cmap=cmap)
ax.add_collection(col)

ax.set(xticks=np.arange(3), yticks=np.arange(10),
       xticklabels=xlabels, yticklabels=ylabels)
ax.set_xticks(np.arange(3+1)-0.5, minor=True)
ax.set_yticks(np.arange(10+1)-0.5, minor=True)
ax.grid(which='minor')


fig.colorbar(col)
plt.show()

Any help would be greatly appreciated!

解决方案

The problem is here that the copied code fills all fields, whereas your code not necessarily has an entry in each box. We have to look up, where each circle has to be plotted:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
import pandas as pd

x= {'ID': {0: 'GO:0002474',
      1: 'GO:0052548',
      2: 'GO:0002483',
      3: 'GO:0043062',
      4: 'GO:0060333'},
     'TERM': {0: 'antigen processing and presentation of peptide antigen via MHC class I',
      1: 'regulation of endopeptidase activity',
      2: 'antigen processing and presentation of endogenous peptide antigen',
      3: 'extracellular structure organization',
      4: 'interferon-gamma-mediated signaling pathway'},
     'Count': {0: 11, 1: 17, 2: 5, 3: 15, 4: 6},
     'Ratio': {0: 18.64, 1: 14.53, 2: 8.47, 3: 12.82, 4: 10.17},
     'pvalue': {0: -15.83, 1: -11.39, 2: -9.67, 3: -9.05, 4: -7.41},
     'qvalue': {0: -11.63, 1: -7.49, 2: -6.52, 3: -5.63, 4: -4.55},
     'Label': {0: 'NODAL', 1: 'GFP', 2: 'NODAL', 3: 'SHARED', 4: 'NODAL'}}

A2780_GOBP= pd.DataFrame(x)
cmap = "plasma"
 
#retrieve unique labels
ylabels = A2780_GOBP["TERM"].unique().tolist()
xlabels = A2780_GOBP["Label"].unique().tolist()
xn = len(xlabels)
yn = len(ylabels)
#retrieve size and color information    
s = A2780_GOBP["Count"].values
c = A2780_GOBP["pvalue"].values


#preparation of the figure with its grid
fig, ax = plt.subplots(figsize=(10, 5))
ax.set_xlim(-0.5, xn-0.5)
ax.set_ylim(-0.5, yn-0.5)
ax.set(xticks=np.arange(xn), yticks=np.arange(yn),
       xticklabels=xlabels, yticklabels=ylabels)
ax.set_xticks(np.arange(xn)-0.5, minor=True)
ax.set_yticks(np.arange(yn)-0.5, minor=True)
ax.grid(which='minor')
#ensure circles are displayed as circles
ax.set_aspect("equal", "box")

#create circles patches and colorbar
R = s/s.max()/2
circles = [plt.Circle((xlabels.index(A2780_GOBP.loc[i, "Label"]), ylabels.index(A2780_GOBP.loc[i, "TERM"])), radius=r) for i, r in enumerate(R)]
col = PatchCollection(circles, array=c, cmap=cmap)
ax.add_collection(col)
fig.colorbar(col)

plt.show()

Sample output:

The code does not check the integrity of your original database, i.e., that each Label-Term pair indeed only occurs once.

这篇关于使用Matplotlib在带有图例的热图中显示不同大小的圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆