Les colorbar divergentes sont couramment employées pour visualiser des données qui possèdent un point médian naturel, telles que la température ou la profondeur. Dans le cas où les données n'ont pas de point médian naturel, il est possible d'utiliser une palette de couleurs divergente en fixant la valeur 0 comme point médian.Example:
Table des matières
Colorbar divergente
Construisons un exemple simple en utilisant matplotlib avec une palette de couleurs divergente :
from pylab import figure, cmimport matplotlib.pyplot as pltimport numpy as npx1_min = -10.0x1_max = 5.0x2_min = -2.0x2_max = 2.0x1, x2 = np.meshgrid(np.arange(x1_min,x1_max, 0.1), np.arange(x2_min,x2_max, 0.1))plt.imshow(x1, origin='lower', cmap='seismic',extent=[x1_min,x1_max,x1_min,x1_max])plt.title("How to set 0 as the middle point \n in a matplotlib diverging colormap ?" , fontsize=12)plt.colorbar()plt.savefig("matplotlib_set_0_middle_point_matpotlib_diverging_colormap_01.png", bbox_inches='tight',dpi=200, facecolor='white')plt.show()plt.close()
donne

zéro n'est pas le point central de la palette de couleurs.
Définir 0 comme point médian
Pour définir 0 comme point médian, une solution consiste à définir une nouvelle norme :
custom_norm=colors.TwoSlopeNorm(vmin=x1_min, vcenter=0., vmax=x1_max)
Exemple:
from pylab import figure, cmimport matplotlib.pyplot as pltimport numpy as npfrom matplotlib import colorsx1_min = -10.0x1_max = 5.0x2_min = -2.0x2_max = 2.0x1, x2 = np.meshgrid(np.arange(x1_min,x1_max, 0.1), np.arange(x2_min,x2_max, 0.1))custom_norm=colors.TwoSlopeNorm(vmin=x1_min, vcenter=0., vmax=x1_max)plt.imshow(x1, origin='lower', cmap='seismic',extent=[x1_min,x1_max,x1_min,x1_max], norm=custom_norm)plt.title("How to set 0 as the middle point \n in a matplotlib diverging colormap ?" , fontsize=12)plt.colorbar()plt.savefig("matplotlib_set_0_middle_point_matpotlib_diverging_colormap_02.png", bbox_inches='tight',dpi=200, facecolor='white')plt.show()plt.close()

Références
| Liens | Site |
|---|---|
| Choosing Colormaps in Matplotlib | matplotlib.org |
| Colormap Normalization | matplotlib.org |
