Rotation of colorbar tick labels in Matplotlib
Last Updated :
24 Jan, 2021
Colorbar is an axis that indicates the mapping of data values to the colors used in plot. The colorbar() function in pyplot module of matplotlib adds a colorbar to a plot indicating the color scale.
Typical Colorbar
Sometimes it is desirable to rotate the ticklabels for better visualization and understanding. To change the rotation of colorbar ticklabels desired angle of rotation is provided in:
- cbar.ax.set_xticklabels, if colorbar orientation is horizontal
- cbar.ax.set_yticklabels, if colorbar orientation is vertical
Positive value of angle corresponds to counterclockwise rotation, while negative value corresponds to clockwise rotation. Also, we can use "vertical" and "horizontal" values for rotation instead of numeric value of angle. These are equivalent to 0° and +90° respectively.
Steps to rotate colorbar ticklabels :
- Plot a figure
- Plot corresponding colorbar
- Provide ticks and ticklabels
- Set rotation of ticklabels to desired angle
Example 1: Following program demonstrates horizontal color bar with 45 degrees rotation of colorbar ticklabels.
Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
# Plot horizontal colorbar
cbar = plt.colorbar(
orientation="horizontal", fraction=0.050)
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
# Rotate colorbar ticklabels by 45 degrees
# anticlockwise
cbar.ax.set_xticklabels(labels, rotation=45)
plt.show()
Output:
Example 2: Following program demonstrates horizontal color bar with -45 degrees rotation of colorbar ticklabels.
Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
# Plot horizontal colorbar
cbar = plt.colorbar(
orientation="horizontal", fraction=0.050)
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
# Rotate colorbar ticklabels by 45 degrees clockwise
cbar.ax.set_xticklabels(labels, rotation=-45)
plt.show()
Output:
Example 3: Following program demonstrates vertical color bar with 30 degrees rotation of colorbar ticklabels.
Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
# Plot vertical colorbar
cbar = plt.colorbar(
orientation="vertical", fraction=0.050)
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
# Rotate colorbar ticklabels by 30 degrees
# anticlockwise
cbar.ax.set_yticklabels(labels, rotation=30)
plt.show()
Output:
Example 4: Following program demonstrates vertical color bar with -30 degrees rotation of colorbar ticklabels.
Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
# Plot vertical colorbar
cbar = plt.colorbar(
orientation="vertical", fraction=0.050)
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
# Rotate colorbar ticklabels by 30 degrees clockwise
cbar.ax.set_yticklabels(labels, rotation=-30)
plt.show()
Output:
Example 5: Following program demonstrates horizontal color bar with vertical rotation of colorbar ticklabels.
Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
# Plot horizontal colorbar
cbar = plt.colorbar(
orientation="horizontal", fraction=0.050)
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
# Rotate colorbar ticklabels by 90 degrees
# anticlockwise using "vertical" value
cbar.ax.set_xticklabels(labels,
rotation="vertical")
plt.show()
Output:
Example 6: Following program demonstrates vertical color bar with 270 degrees rotation of colorbar ticklabels.
Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Plot image
a = np.random.random((10, 10))
plt.imshow(a, cmap='gray')
# Plot vertical colorbar
cbar = plt.colorbar(
orientation="vertical", fraction=0.050)
# Set ticklabels
labels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
0.7, 0.8, 0.9, 1]
cbar.set_ticks(labels)
# Rotate colorbar ticklabels by 270 degrees
# anticlockwise
cbar.ax.set_yticklabels(labels, rotation=270)
plt.show()
Output:
Similar Reads
Python Matplotlib: Decrease Size of Colorbar Labels Matplotlib is a widely used library in Python for creating static, interactive, and animated visualizations. When creating plots that include a colorbar, adjusting the size of the colorbar labels can be crucial for readability and presentation. This article explores three different methods to decrea
4 min read
Rotate axis tick labels in Seaborn and Matplotlib Seaborn and Matplotlib both are commonly used libraries for data visualization in Python. Â We can draw various types of plots using Matplotlib like scatter, line, bar, histogram, and many more. On the other hand, Seaborn provides a variety of visualization patterns. It uses easy syntax and has easil
3 min read
How to change colorbar labels in matplotlib ? 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=Non
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
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