matplotlib: colorbars and its text labels

import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import ListedColormap #discrete color scheme cMap = ListedColormap([‘white’, ‘green’, ‘blue’,’red’]) #data np.random.seed(42) data = np.random.rand(4, 4) fig, ax = plt.subplots() heatmap = ax.pcolor(data, cmap=cMap) #legend cbar = plt.colorbar(heatmap) cbar.ax.get_yaxis().set_ticks([]) for j, lab in enumerate([‘$0$’,’$1$’,’$2$’,’$>3$’]): cbar.ax.text(.5, (2 * j + 1) / 8.0, lab, ha=”center”, va=”center”) … Read more

How to have one colorbar for all subplots

Just place the colorbar in its own axis and use subplots_adjust to make room for it. As a quick example: import numpy as np import matplotlib.pyplot as plt fig, axes = plt.subplots(nrows=2, ncols=2) for ax in axes.flat: im = ax.imshow(np.random.random((10,10)), vmin=0, vmax=1) fig.subplots_adjust(right=0.8) cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7]) fig.colorbar(im, cax=cbar_ax) plt.show() Note that the … Read more