【问题标题】:Single Column Heatmap with Circle of Different Sizes具有不同大小圆圈的单列热图
【发布时间】:2020-12-12 13:22:18
【问题描述】:

我从此处提供的示例进行扩展。 Heatmap with circles indicating size of population

但是,我不知道如何将 NumPy 数组与 PathCollector 一起使用来创建一个热图,该热图生成一个包含 20 个变量(Term)的单列,其中的圆圈表示大小(Number_Protein)和颜色(P_value_abs)。

这是我迄今为止所得到的,任何帮助将不胜感激。

ylabels = Shared["Term"]

xlabels = ["Overlap"]

x, y = np.meshgrid(1, 20)
s = Shared["Number_Protein"]
c = Shared["P_value_abs"]

fig, ax = plt.subplots()

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

ax.set(xticklabels=xlabels, yticklabels=ylabels)
fig.colorbar(col)
plt.show()

这个问题已经回答了,但我只是想展示最终产品,以防有人好奇。

Final Product

【问题讨论】:

  • 不确定如何发布没有图片的数据框?
  • 你可以添加print(Shared.to_dict())的输出

标签: python numpy matplotlib heatmap


【解决方案1】:

这个问题的确切目标有点难以猜测。这是一个尝试:

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

Shared = pd.DataFrame({'Term': ['Term{i}' for i in range(1, 7)],
                       'Number_Protein': np.random.randint(150, 220, 6),
                       'P_value_abs': np.random.uniform(50, 95, 6)})
ylabels = Shared["Term"]
xlabels = ["Overlap"]

s = Shared["Number_Protein"]
c = Shared["P_value_abs"]
norm = plt.Normalize(c.min(), c.max())

fig, ax = plt.subplots()

R = s / s.max() / 2
circles = [plt.Circle((0, i), radius=r) for i, r in enumerate(R)]
col = PatchCollection(circles, array=c, cmap="coolwarm", norm=norm)
ax.add_collection(col)

ax.set_xticks([0])
ax.set_xticklabels(xlabels)
ax.set_yticks(range(len(R)))
ax.set_yticklabels(ylabels)
ax.set_xlim(-0.5, 0.5)
ax.set_ylim(-0.5, len(ylabels)-0.5 )
ax.set_aspect('equal')
fig.colorbar(col)
plt.show()

这将创建一个圆的半径与“Number_Protein”成正比且颜色与“P_value_abs”成正比的图。请注意,当颜色值不是很好地介于 0 和 1 之间时,需要 norm 将原始值转换为该范围。

【讨论】:

  • 太棒了。这看起来正是我正在寻找的。我会用我的数据测试它并让你知道。谢谢。
  • 如果另一列指示哪个圆圈属于哪个组,如何添加多列。
  • 有点不清楚你到底需要什么。也许您可以使用示例数据创建一个新问题以及结果应该是什么样的?
  • stackoverflow.com/questions/65631607/…> 已经准备好了 :) 谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-03-14
  • 2021-04-14
  • 2015-06-14
  • 2021-01-18
  • 2019-10-30
  • 1970-01-01
  • 1970-01-01
  • 2022-11-03
相关资源
最近更新 更多