I want to make 4 imshow
subplots but all of them share the same colormap. Matplotlib automatically adjusts the scale on the colormap depending on the entries of the matrices. For example, if one of my matrices has all entires as 10 and the other one has all entries equal to 5 and I use the Greys
colormap then one of my subplots should be completely black and the other one should be completely grey. But both of them end up becoming completely black. How to make all the subplots share the same scale on the colormap?
To have one color bar for all subplots with Python, we can use matplotlib's subplots_adjust and colorbar methods. to call subplots_adjust on the fig subplot with the right argument to adjust the position of the subplots.
Use imshow() method to display the data as an image, i.e., on a 2D regular raster. Create a colorbar for a ScalarMappable instance, im. Set colorbar label using set_label() method. To display the figure, use show() method.
By default, imshow normalizes the data to its min and max. You can control this with either the vmin and vmax arguments or with the norm argument (if you want a non-linear scaling).
figure() − Creates a new figure or activates an existing figure. plt. subplots() − Creates a figure and a set of subplots.
To get this right you need to have all the images with the same intensity scale, otherwise the colorbar()
colours are meaningless. To do that, use the vmin
and vmax
arguments of imshow()
, and make sure they are the same for all your images.
E.g., if the range of values you want to show goes from 0 to 10, you can use the following:
import pylab as plt import numpy as np my_image1 = np.linspace(0, 10, 10000).reshape(100,100) my_image2 = np.sqrt(my_image1.T) + 3 subplot(1, 2, 1) plt.imshow(my_image1, vmin=0, vmax=10, cmap='jet', aspect='auto') plt.subplot(1, 2, 2) plt.imshow(my_image2, vmin=0, vmax=10, cmap='jet', aspect='auto') plt.colorbar()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With