Reputation: 170
I am extending from an example provided here. Heatmap with circles indicating size of population
However, I am lost on how to use a NumPy array with PathCollector to create a heatmap that produces a single columns of 20 variables(Term) with circles that indicated size (Number_Protein) and Colour (P_value_abs).
This is where I have gotten thus far, any help would be appreciated.
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()
This question was answered, but I just wanted to show the final product incase someone was curious.
Upvotes: 0
Views: 357
Reputation: 80409
The precise goal of the question is a bit hard to guess. Here is an attempt:
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()
This creates a plot with the circles' radius proportional to "Number_Protein" and the color relative to "P_value_abs". Note that when the color values aren't nicely between zero and one, a norm
is needed to convert the original values to that range.
Upvotes: 1