Scientific notation colorbar in matplotlib

You could use colorbar‘s format parameter: import matplotlib.pyplot as plt import numpy as np import matplotlib.ticker as ticker img = np.random.randn(300,300) myplot = plt.imshow(img) def fmt(x, pos): a, b = ‘{:.2e}’.format(x).split(‘e’) b = int(b) return r’${} \times 10^{{{}}}$’.format(a, b) plt.colorbar(myplot, format=ticker.FuncFormatter(fmt)) plt.show()

How can I create a standard colorbar for a series of plots in python

Not to steal @ianilis’s answer, but I wanted to add an example… There are multiple ways, but the simplest is just to specify the vmin and vmax kwargs to imshow. Alternately, you can make a matplotlib.cm.Colormap instance and specify it, but that’s more complicated than necessary for simple cases. Here’s a quick example with a … Read more

How to animate the colorbar in matplotlib

While I’m not sure how to do this specifically using an ArtistAnimation, using a FuncAnimation is fairly straightforward. If I make the following modifications to your “naive” version 1 it works. Modified Version 1 import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation from mpl_toolkits.axes_grid1 import make_axes_locatable fig = plt.figure() ax = … Read more

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

tech