Imshow with two colorbars under Matplotlib - GeeksforGeeks

24 Jan, 2021
In this article, we will learn how to use Imshow with two colorbars under Matplotlib. Let’s discuss some concepts :
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002.
One of the greatest benefits of visualization is that it allows us visual access to huge amounts of data in easily digestible visuals. Matplotlib consists of several plots like line, bar, scatter, histogram etc.
The imshow() function in pyplot module of matplotlib library is used to display data as an image; i.e. on a 2D regular raster.
The colorbar() function in pyplot module of matplotlib adds a colorbar to a plot indicating the color scale.
A simple Imshow() with one colorbar
Python3
# import librariesimport matplotlib.pyplot as pltimport numpy as np  # create image = 10x10 arrayimg = np.random.randint(-100, 100, (10, 10))  # make plotfig, ax = plt.subplots()  # show imageshw = ax.imshow(img)  # make barbar = plt.colorbar(shw)  # show plot with labelsplt.xlabel('X Label')plt.ylabel('Y Label')bar.set_label('ColorBar')plt.show()
Output :
In the above output, we can see that there is one colorbar with values ranges from -100 to 100. This is not looking effective and not clear the difference of small positive values to larger positive values similarly not clear the difference of small negative values to larger negative values. Here, we divide colorbar in two parts :
one with positive values
one with negative values
With different colors, which help us to understand the plot clearly and effectively. Below all steps are mentioned for such work.
Steps Needed:
Import libraries (matplotlib)Create / load image dataMasked array to positive and negative valuesMake plot using subplot() methodShow image using imshow() methodMake bars using matplotlib.pyplot.colorbar() methodShow plot with labels
Import libraries (matplotlib)
Create / load image data
Masked array to positive and negative values
Make plot using subplot() method
Show image using imshow() method
Make bars using matplotlib.pyplot.colorbar() method
Show plot with labels
Example:
Python3
# import librariesimport matplotlib.pyplot as pltimport numpy as npfrom numpy.ma import masked_array  # create image = 10x10 arrayimg = np.random.randint(-100, 100, (10, 10))  # masked array to positive and negative valuesneg_img = masked_array(img, img >= 0)pos_img = masked_array(img, img < 0)  # make plotfig, ax = plt.subplots()  # show imageshw1 = ax.imshow(neg_img, cmap=plt.cm.Reds)shw2 = ax.imshow(pos_img, cmap=plt.cm.winter)  # make barsbar1 = plt.colorbar(shw1)bar2 = plt.colorbar(shw2)  # show plot with labelsplt.xlabel('X Label')plt.ylabel('Y Label')bar1.set_label('ColorBar 1')bar2.set_label('ColorBar 2')plt.show()
Output :
Picked
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()