How to change colorbar labels in matplotlib ? - GeeksforGeeks

29 Dec, 2021
In this article, we are going to see how to change color bar labels in matplotlib using Python.
The colorbar() function is used to plot the color bar which belongs to the pyplot module of matplotlib adds a colorbar to a plot indicating the color scale.
Syntax: matplotlib.pyplot.colorbar(mappable=None, cax=None, ax=None, **kwarg)
Parameters:
ax: This parameter is an optional parameter and it contains Axes or list of Axes.
**kwarg(keyword arguments): This parameter is an optional parameter and are of two kinds:
colorbar properties:
extend:{‘neither’, ‘both’, ‘min’, ‘max’} makes pointed end(s) for out-of-range values.
label:The label on the colorbar’s long axis.
ticks:None or list of ticks or Locator.
Returns:colorbar which is an instance of the class ‘matplotlib.colorbar.Colorbar’.     
To create the colorbar we will use color() methods, for this, we will create the dataset and then use a scatterplot for demonstration.
Python3
# Python Program illustrating# pyplot.colorbar() methodimport numpy as npimport matplotlib.pyplot as plt # Dataset# List of total number of items purchased# from each productspurchaseCount = [100, 200, 150, 23, 30, 50,                156, 32, 67, 89] # List of total likes of 10 productslikes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45] # List of Like/Dislike ratio of 10 productsratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,        1.28, 1.09, 1.02] # scatterplotplt.scatter(x=purchaseCount, y=likes, c=ratio, cmap="summer") plt.colorbar()plt.show()
Output:
To change the label’s font size we will use ax.tick_params() methods which increase the font of the labels.
Python3
# Python Program illustrating# pyplot.colorbar() methodimport numpy as npimport matplotlib.pyplot as plt # Dataset# List of total number of items purchased# from each productspurchaseCount = [100, 200, 150, 23, 30, 50,                156, 32, 67, 89] # List of total likes of 10 productslikes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45] # List of Like/Dislike ratio of 10 productsratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,        1.28, 1.09, 1.02] # scatterplotplt.scatter(x=purchaseCount, y=likes, c=ratio,            cmap="summer") plt.colorbar().ax.tick_params(labelsize=10) plt.show()
Output:
To rotate the colorbar labels we will use set_xticklabels() and set_yticklabels() methods for horizontal and vertical.
Example 1: Changing position horizontally
Python3
# Python Program illustrating# pyplot.colorbar() methodimport numpy as npimport matplotlib.pyplot as plt # Dataset# List of total number of items purchased# from each productspurchaseCount = [100, 200, 150, 23, 30, 50,                156, 32, 67, 89] # List of total likes of 10 productslikes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45] # List of Like/Dislike ratio of 10 productsratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,        1.28, 1.09, 1.02] # scatterplotplt.scatter(x=purchaseCount, y=likes, c=ratio,            cmap="summer") plt.colorbar().ax.set_ylabel('Label',                             rotation=120) plt.show()
Output:
Example 2: Changing position vertically
Python3
# Python Program illustrating# pyplot.colorbar() methodimport numpy as npimport matplotlib.pyplot as plt # Dataset# List of total number of items purchased# from each productspurchaseCount = [100, 200, 150, 23, 30, 50,                156, 32, 67, 89] # List of total likes of 10 productslikes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45] # List of Like/Dislike ratio of 10 productsratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,        1.28, 1.09, 1.02] # scatterplotplt.scatter(x=purchaseCount, y=likes, c=ratio, cmap="summer")plt.colorbar(orientation="horizontal").ax.set_xticklabels(ratio, rotation=45) plt.show()
Output:
anikakapoor
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?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Python | Get unique values from a list
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()