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
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.
- 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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.