Imshow avec deux colorbars sous matplotlib

Published: 05 avril 2017

DMCA.com Protection Status

Exemple de comment avoir une figure matplotlib avec deux colorbars (exemple inspiré du code source suivant)

Two colorbars Imshow matplotlib
Two colorbars Imshow matplotlib

On a ici de plus ajusté la taille de la colorbar à la figure (voir)

from mpl_toolkits.axes_grid1 import make_axes_locatable
from numpy.ma import masked_array

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm

v1 = -1+2*np.random.rand(20,20)

v1a = masked_array(v1,v1<0)
v1b = masked_array(v1,v1>=0)

fig,ax = plt.subplots()

pa = ax.imshow(v1a,interpolation='nearest',cmap=cm.Reds)
pb = ax.imshow(v1b,interpolation='nearest',cmap=cm.winter)

plt.xlabel('Day')
plt.ylabel('Depth')
plt.title('Matplotlib imshow with two colormaps')

divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.3)
cba = plt.colorbar(pa, cax=cax)

cax = divider.append_axes("right", size="5%", pad=1.0)
cbb = plt.colorbar(pb, cax=cax)

cba.set_label('positive')
cbb.set_label('negative')

plt.savefig("matplotlib_two_colormaps.png", bbox_inches='tight')
plt.show()

Références

Image

of