I have two numpy arrays where one should be used to determine the radius of matplotlib.patches.Circle
and the other should be used to determine its colour from a diverging colourmap. Positive values in data
should be orange and negative values blue.
I wanted to plot the circle in one figure but cmap
doesn’t seem to assign the colour gradient to data correctly and the x, y coordinates also seem off.
import numpy as np
import seaborn as sns
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
# use to determine colour
data = np.array([
[0.02, 0.0098, 0.0023, 0.0016, -0.098, -0.14, -0.18, -0.23, -0.29, -0.35],
[0.09, 0.0432, 0.0132, 0.0095, 0.0004, -0.04, -0.067, -0.21, -0.25, -0.32],
[0.1, 0.0622, 0.04327, 0.0098, 0.0001, -0.04, -0.07, -0.19, -0.23, -0.3],
[0.13, 0.092, 0.042, 0.017, 0.004, -0.04, -0.087, -0.12, -0.23, -0.29],
[0.1425, 0.12, 0.0992, 0.035, 0.0067, -0.09, -0.1, -0.13, -0.2, -0.25],
[0.1964, 0.143, 0.121, 0.0921, 0.019, 0.09, -0.009, -0.012, -0.19, -0.2],
[0.2, 0.19, 0.1798, 0.176, 0.1453, 0.094, -0.001, -0.09, -0.1, -0.12],
[0.22, 0.20, 0.19, 0.12, 0.1199, 0.1187, -0.03, -0.021, -0.012, -0.09],
[0.3, 0.29, 0.25, 0.12, 0.117, 0.113, -0.002, -0.009, -0.01, -0.018],
[0.32, 0.31, 0.24, 0.23, 0.18, 0.15, 0.065, -0.00056, -0.0054, -0.009],
])
# use to determine radius
significance = np.array([
[0.02, 0.0098, 0.0023, 0.0016, 0.98, 0.14, 0.18, 0.03, 0.09, 0.005],
[0.09, 0.0432, 0.012, 0.0095, 0.9, 0.05, 0.008, 0.01, 0.005, 0.002],
[0.001, 0.000622, 0.004327, 0.98, 0.1, 0.0004, 0.0007, 0.00019, 0.0023, 0.003],
[0.0013, 0.00092, 0.00042, 0.00017, 0.4, 0.04, 0.087, 0.0012, 0.0023, 0.0029],
[0.0005, 0.001, 0.0002, 0.35, 0.67, 0.9, 0.01, 0.003, 0.002, 0.005],
[0.0001964, 0.000143, 0.0000121, 0.00921, 0.19, 0.9, 0.009, 0.00012, 0.0019, 0.002],
[0.002, 0.0019, 0.0001798, 0.176, 0.1453, 0.094, 0.001, 0.0009, 0.001, 0.0012],
[0.002, 0.0020, 0.00019, 0.0012, 0.1199, 0.1187, 0.03, 0.00021, 0.012, 0.0009],
[0.003, 0.0009, 0.0025, 0.0012, 0.117, 0.113, 0.002, 0.0009, 0.00001, 0.00018],
[0.0002, 0.001, 0.0004, 0.0023, 0.008, 0.15, 0.65, 0.56, 0.054, 0.009],
])
# collect circles
patches = []
cmap = sns.color_palette('icefire', as_cmap=True)
f, ax = plt.subplots(figsize=(16, 15))
# loop over dimensions
for i in range(data.shape[0]):
for j in range(data.shape[0]):
p = significance[i, j]
d = data[i, j]
# only print circle if p <0.05
if p < 0.05:
radius = (1 - p / 0.1) / 2
color = cmap(d/0.1)
circle = Circle((i, j), radius, color=color, alpha=1)
patches.append(circle)
collection = PatchCollection(patches, match_original=True)
ax.add_collection(collection)
ax.set_aspect('equal')
ax.set_xlim(-0.55, significance.shape[0] - 0.45)
ax.set_ylim(-0.55, significance.shape[1] - 0.45)
plt.show()
What I wanted the colour and coordinates look like is when I plot the data using plt.imshow
. Any ideas on how to fix the gradient and coordinates in the circles plot??
plt.imshow(data, cmap=cmap)
plt.show()
user9361 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.