Imshow with two colorbars under Matplotlib Last Updated : 24 Jan, 2021 Comments Improve Suggest changes Like Article Like Report 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 libraries import matplotlib.pyplot as plt import numpy as np # create image = 10x10 array img = np.random.randint(-100, 100, (10, 10)) # make plot fig, ax = plt.subplots() # show image shw = ax.imshow(img) # make bar bar = plt.colorbar(shw) # show plot with labels plt.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 valuesone 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 Example: Python3 # import libraries import matplotlib.pyplot as plt import numpy as np from numpy.ma import masked_array # create image = 10x10 array img = np.random.randint(-100, 100, (10, 10)) # masked array to positive and negative values neg_img = masked_array(img, img >= 0) pos_img = masked_array(img, img < 0) # make plot fig, ax = plt.subplots() # show image shw1 = ax.imshow(neg_img, cmap=plt.cm.Reds) shw2 = ax.imshow(pos_img, cmap=plt.cm.winter) # make bars bar1 = plt.colorbar(shw1) bar2 = plt.colorbar(shw2) # show plot with labels plt.xlabel('X Label') plt.ylabel('Y Label') bar1.set_label('ColorBar 1') bar2.set_label('ColorBar 2') plt.show() Output : Comment More infoAdvertise with us Next Article Imshow with two colorbars under Matplotlib D deepanshu_rustagi Follow Improve Article Tags : Python Python-matplotlib Practice Tags : python Similar Reads Positioning the colorbar in Matplotlib Matplotlib is a plotting library in Python programming language and it's by default numerical mathematics extension of NumPy library in python language. While programming in python language we use the matplotlib library package for graph and histogram visualizations. But while plotting histogram usi 3 min read Set Colorbar Range in matplotlib When working with plots, colorbars are often used to show how data values correspond to colors on the plot. Sometimes the default color scale might not represent the data well or might not fit the range we need. In such cases, setting a specific colorbar range becomes essential. In this article, weâ 4 min read Matplotlib.colors.to_rgb() in Python 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. matplotlib.colors.to_rgb() The matplotlib.colors.to_rgb() function is used convert c (i 3 min read Matplotlib.colors.to_rgba() in Python 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. matplotlib.colors.to_rgba() The matplotlib.colors.to_rgba() function is used convert c( 3 min read Set Matplotlib colorbar size to match graph Colorbar size that match graph or image is required to get good visualize effect. This can be achieved using any one of following approaches. Use fraction parameter to match graph Fraction parameter in colorbar() is used to set the size of colorbar in Python. Using this we can match colorbar size to 4 min read Like