2

I am plotting a heatmap that have many values set to zero, a few that are very low (0.001 to 0.05) and a few that are high (>0.9 up to 1). Is it possible to set a non-uniform colorbar spacing so the data can be better understood? How can that be done?

data = {(0, 33): 0.002, (0, 34): 0.007, (0, 35): 0.015, (0, 36): 0.014, (0, 37): 0.943, (0, 38): 0.01, (0, 51): 0.007, (1, 33): 0.002, (1, 34): 0.002, (1, 35): 0.005, (1, 36): 0.005, (1, 37): 0.977, (1, 38): 0.006, (1, 51): 0.003, (2, 34): 0.002, (2, 35): 0.003, (2, 36): 0.003, (2, 37): 0.99, (2, 38): 0.002, (3, 33): 0.002, (3, 34): 0.003, (3, 35): 0.005, (3, 36): 0.007, (3, 37): 0.978, (3, 38): 0.005, (3, 51): 0.001, (4, 37): 1.0, (5, 37): 1.0, (12, 33): 0.003, (12, 34): 0.004, (12, 35): 0.018, (12, 36): 0.017, (12, 37): 0.931, (12, 38): 0.013, (12, 49): 0.001, (12, 51): 0.012, (13, 33): 0.003, (13, 34): 0.004, (13, 35): 0.009, (13, 36): 0.013, (13, 37): 0.944, (13, 38): 0.014, (13, 51): 0.012, (14, 34): 0.001, (14, 35): 0.002, (14, 36): 0.003, (14, 37): 0.993, (14, 38): 0.001, (15, 34): 0.001, (15, 35): 0.002, (15, 36): 0.003, (15, 37): 0.992, (15, 38): 0.001, (16, 37): 0.996, (16, 38): 0.004, (17, 37): 1.0, (18, 37): 1.0, (19, 37): 1.0, (20, 37): 1.0, (21, 37): 1.0, (22, 37): 1.0, (23, 37): 1.0, (24, 33): 0.004, (24, 34): 0.007, (24, 35): 0.032, (24, 36): 0.035, (24, 37): 0.903, (24, 38): 0.016, (24, 51): 0.003, (25, 33): 0.004, (25, 34): 0.006, (25, 35): 0.029, (25, 36): 0.034, (25, 37): 0.907, (25, 38): 0.016, (25, 51): 0.003, (26, 35): 0.004, (26, 36): 0.007, (26, 37): 0.972, (26, 38): 0.017, (27, 33): 0.004, (27, 34): 0.005, (27, 35): 0.007, (27, 36): 0.008, (27, 37): 0.961, (27, 38): 0.016, (28, 33): 0.005, (28, 34): 0.006, (28, 35): 0.008, (28, 36): 0.01, (28, 37): 0.961, (28, 38): 0.01, (29, 33): 0.002, (29, 34): 0.003, (29, 35): 0.023, (29, 36): 0.016, (29, 37): 0.943, (29, 38): 0.012, (30, 33): 0.001, (30, 35): 0.01, (30, 36): 0.001, (30, 37): 0.985, (30, 38): 0.004, (35, 37): 1.0, (36, 37): 1.0, (37, 37): 1.0, (38, 38): 1.0, (39, 49): 0.013, (39, 50): 0.012, (39, 51): 0.975, (40, 47): 0.18, (40, 49): 0.02, (40, 50): 0.126, (40, 51): 0.674, (41, 49): 0.012, (41, 50): 0.022, (41, 51): 0.966}

i_set, j_set = set(), set()
for k in data.keys():
    i_set.add(k[0])
    j_set.add(k[1])

i_set = list(i_set)
i_set.sort()
j_set = list(j_set)
j_set.sort()

# Make array
array = []
for i in i_set:
    row = []
    for j in j_set:
        row.append( data.get((i,j), 0) )
    array.append(row)

array = np.array(array)

# plot
fig, ax = plt.subplots()
im = ax.imshow(array)

# We want to show all ticks...
ax.set_xticks(np.arange(len(j_set)))
ax.set_yticks(np.arange(len(i_set)))
# ... and label them with the respective list entries
ax.set_xticklabels(j_set)
ax.set_yticklabels(i_set)

# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")

# Loop over data dimensions and create text annotations.
for i in range(len(i_set)):
    for j in range(len(j_set)):
        if array[i,j] != 0:
            text = ax.text(j, i, array[i, j],
                ha="center", va="center", color="w", fontsize='xx-small')

fig.tight_layout()
plt.show()

Furthermore, the top and bottom rows are "cut in half" instead of shown in full. How can I fix that?

This is the figure I get with the code I'm showing here.

EDIT The last question, of how to fix the cut in half squares was solved. Still, the question on how to make the colorbar vary non-uniformly is open!

YamiOmar88
  • 1,336
  • 1
  • 8
  • 20
  • 2
    See this question for the problem of the top and bottom rows cut in half: https://stackoverflow.com/questions/56942670/matplotlib-seaborn-first-and-last-row-cut-in-half-of-heatmap-plot – Diziet Asahi Jul 23 '20 at 08:46
  • Thank you for the suggestion. Indeed, upgrading matplotlib solved that issue! – YamiOmar88 Jul 23 '20 at 10:11
  • Does this answer your question? [matplotlib/seaborn: first and last row cut in half of heatmap plot](https://stackoverflow.com/questions/56942670/matplotlib-seaborn-first-and-last-row-cut-in-half-of-heatmap-plot) – Trenton McKinney Jul 23 '20 at 17:54
  • I'm voting to close this question, as the OP has indicated the solution from the duplicate resolved the issue. – Trenton McKinney Jul 23 '20 at 17:55
  • I resolved only part of the question. Please read the question carefully. – YamiOmar88 Jul 24 '20 at 06:15

0 Answers0