I'm trying to plot a seaborn heatmap centered on 0. So I can see the divergence of the values on the positive and negative range. The problem is that near 0 the values change too smoothly, on the other side the absolute values, either for the positive or the negative range are really large.
I'm using the code
params = {'font.size': 18, 'xtick.labelsize':16, ytick.labelsize':16}
plt.rcParams.update(params)
plt.figure(figsize=(24,8))
mask = dfs.isnull()
cmap = sns.diverging_palette(255, 0, s=99, sep=1, as_cmap=True)
sns.heatmap(dfs,cmap=cmap, mask=mask, center=0, annot=labelS, fmt='')
plt.show()
So, I expected that positive values to be red, and the negatives to be blue. I created a 2D numpy array so I can write the signal of the data. The problem is that, when I plot it, near 0, are negative values that are still red.
The heatmap result from the code above is shown here:
Edit1: I tryed this answer:
Defining the midpoint of a colormap in matplotlib,
with the midpoint suggested (1 - vmax / (vmax + abs(vmin)) = 0.9481981715613719)
And neither the option of shifted or shrunk cmap works, maybe if I know the best star and end point of the function it will work better, but as I don't know how to choose it different than 0 and 1, the look the same. I plot the heatmap with the actual values in the figures, so it will be better to see.
I think because the range of values are large, and there are some between 0 and 1, while vmax=217 and vmin=-3981, the divergence isn't fast enough. And the divergence of the positive color is different from the divergence of the negative range.
Maybe there is a best value for start and end point, the problem is that how can I do it from the data?
@DaveTheScientist said that this solution fails when the midpoint is equal to 0 or 1, and he proposes a new function, my midpoint is really near 1, but his solution still didn't work for me, I got the same result as the original answer from @Paul H.
And if the value of each cell is not showed, i'm not able to differentiate the missing values, from the near 0 values.
Edit 2: Now I tryed the options 1 and 2 from here Shifted colorbar matplotlib. And there are even more problems, because of the missing values, they are getting colors as the other values. And the problems with the values near 0 still appears.
My dataset is a pandas pivot table(dfs) with missing values(nan), so it's not regular, (maybe this is the problem?)
0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.85 0.9
0.59 nan 0.409447 0.403747 0.220221 0.198353 0.112916 1.70769 -188.474 -1803.68
0.57 nan nan 0.402517 0.393347 0.206475 0.182728 2.3221 -180.016 -1814.15
0.55 nan nan 0.403747 0.229443 0.211342 0.114802 3.02297 -203.211 -1787.95
0.53 nan 0.410742 0.404906 0.226998 0.129954 0.113135 -0.759108 -183.026 -1837.94
0.51 nan nan 0.401211 0.39512 0.20654 0.189979 1.02238 -178.454 -1584.25
0.49 nan nan 0.404906 0.396786 0.209138 0.117177 -0.472419 -173.616 -1750.06
0.47 nan 0.410139 0.405985 0.228938 0.128596 0.119513 2.03246 -241.663 -1751.43
0.45 nan 0.411318 0.404906 0.398352 0.206402 0.120057 -5.65621 -234.121 -1669.31
0.43 nan nan 0.405985 0.398352 0.221434 0.203799 -3.45825 -247.245 -1540.45
0.41 nan nan 0.409447 0.402516 0.213089 0.191273 -8.74121 -290.459 -1501.15
0.39 nan nan 0.406968 0.402516 0.226998 0.12801 -3.31603 -260.139 -1460.85
0.37 nan nan 0.406968 0.403747 0.227574 0.132975 -4.60131 -339.783 -1423.44
0.35 nan nan 0.410139 0.403747 0.393347 0.209181 -10.0318 -309.951 -1264.69
0.33 nan nan 0.410741 0.403747 0.230069 0.141903 -15.315 -366.672 -1106.8
0.31 nan nan 0.413946 0.406968 0.24464 0.219125 -22.3067 -371.801 -3981.65
0.29 nan nan 0.413761 0.408701 0.401211 0.223775 -19.8598 -429.873 -812.076
0.27 nan nan 0.412663 0.405985 0.396786 0.221357 -19.9852 -420.288 -661.54
0.25 nan nan 0.413762 0.410139 0.24378 0.220389 -45.1834 -407.948 -561.944
0.23 nan nan nan 0.412253 0.402516 0.229078 -83.3562 -384.158 -222.658
0.21 nan nan 0.41405 0.412986 0.403747 0.401211 -129.443 -407.312 -8.51213
0.19 nan nan nan 0.414211 0.4087 0.80159 -128.428 -226.756 204.561
0.17 nan nan nan 0.413975 0.412986 -2.14317 -107.155 106.968 217.525
0.15 nan nan nan nan 0.414136 -6.2662 107.339 154.627 128.412
0.13 nan nan -4.84622 16.165 31.0498 29.0702 22.9361 23.9982 18.0343
0.125 nan -8.68298 17.7774 21.5409 21.1447 23.1349 12.5357 19.238 13.954
0.12 5.19241 20.5002 25.1215 18.5761 21.3382 15.3316 2.84265 2.37559 11.0637
0.115 26.9932 35.8498 30.6433 10.9662 -0.579587 4.75105 1.82187 5.57865 1.94192
0.11 33.4479 34.3506 22.2485 13.1617 12.6101 12.3581 1.8572 2.91308 2.91308
0.105 39.4335 37.4019 37.4019 26.5404 15.0971 15.0971 -871.345 -871.345 1.14922
0.1 30.1349 30.1349 27.678 27.678 19.0532 19.0532 8.53414 8.53414 8.53414
0.095 34.9027 34.9027 16.1035 16.1035 16.1035 14.4113 14.4113 14.4113 14.4113
0.09 32.3556 32.3556 32.3556 5.51217 5.51217 5.51217 5.51217 5.51217 5.51217
0.085 -479.572 -479.572 -479.572 1.9069 1.9069 1.9069 1.9069 1.9069 1.9069
0.08 -0.14278 -0.14278 -0.14278 -0.14278 -0.14278 -0.14278 -0.14278 -0.14278 -0.14278
0.075 4.3636 4.3636 4.3636 4.3636 4.3636 4.3636 4.3636 4.3636 4.3636
0.07 -0.103964 -0.103964 -0.103964 -0.103964 -0.103964 -0.103964 -0.103964 -0.103964 -0.103964
0.065 -3.51647 -3.51647 -3.51647 -3.51647 -3.51647 -3.51647 -3.51647 -3.51647 -3.51647
0.06 -5.33304 -5.33304 -5.33304 -5.33304 -5.33304 -5.33304 -5.33304 -5.33304 -5.33304
0.055 -1.01983 -1.01983 -1.01983 -1.01983 -1.01983 -1.01983 -1.01983 -1.01983 -1.01983
0.05 -0.0490618 -0.0490618 -0.0490618 -0.0490618 -0.0490618 -0.0490618 -0.0490618 -0.0490618 -0.0490618
0.045 -3.03683 -3.03683 -3.03683 -3.03683 -3.03683 -3.03683 -3.03683 -3.03683 -3.03683
0.04 -2.75252 -2.75252 -2.75252 -2.75252 -2.75252 -2.75252 -2.75252 -2.75252 -2.75252
0.035 -3.31855 -3.31855 -3.31855 -3.31855 -3.31855 -3.31855 -3.31855 -3.31855 -3.31855
0.03 0.0711071 0.0711071 0.0711071 0.0711071 0.0711071 0.0711071 0.0711071 0.0711071 0.0711071
0.025 0.0669509 0.0669509 0.0669509 0.0669509 0.0669509 0.0669509 0.0669509 0.0669509 0.0669509
0.02 0.0587947 0.0587947 0.0587947 0.0587947 0.0587947 0.0587947 0.0587947 0.0587947 0.0587947
0.015 -4.69897 -4.69897 -4.69897 -4.69897 -4.69897 -4.69897 -4.69897 -4.69897 -4.69897
0.01 -4.44004 -4.44004 -4.44004 -4.44004 -4.44004 -4.44004 -4.44004 -4.44004 -4.44004
0.005 -3.66247 -3.66247 -3.66247 -3.66247 -3.66247 -3.66247 -3.66247 -3.66247 -3.66247
As you can see, the divergence it's not centered on 0.