Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imshow subplots with the same colorbar

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?

like image 407
lovespeed Avatar asked Aug 01 '13 09:08

lovespeed


People also ask

How do I get one color bar for all subplots?

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.

How do I show Colorbar on Imshow?

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.

Does Imshow normalize image?

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).

What is the difference between PLT figure and PLT subplots?

figure() − Creates a new figure or activates an existing figure. plt. subplots() − Creates a figure and a set of subplots.


1 Answers

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() 

enter image description here

like image 164
tiago Avatar answered Sep 28 '22 11:09

tiago