- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to shift the colorbar position to right in matplotlib?
To shift the colorbar position to right in matplotlib, we can take the following steps −
Steps
Import numpy and matplotlib.
Set the figure size and adjust the padding between and around the subplots.
Initialize a variable N to store the number of sample data.
Create x and y data points using numpy.
Create a scatter plot using scatter() method with x and y data points.
Add a colorbar to a plot, use the pad value for horizontal shift towards right or left.
To display the figure, use show() method.
Example
# Import numpy and matplotlib import numpy as np from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True N = 100 # Create x and y data points x = np.random.rand(N) y = np.random.rand(N) # Scatter plot with x and y data points s = plt.scatter(x, y, c=x, cmap='hot', marker='*') # Add a colorbar with pad value plt.colorbar(s, shrink=0.9, pad=0.1) # Display the plot plt.show()
Output
It will produce the following output −
If you would like to set the colorbar to the left, then use the location parameter as shown below −
plt.colorbar(s, shrink=0.9, pad=0.1, location="left")
With this line in the code, we will have the output as follows −
- Related Articles
- How to animate the colorbar in Matplotlib?
- How to decrease colorbar width in Matplotlib?
- How to add Matplotlib Colorbar Ticks?
- How to retrieve colorbar instance from figure in Matplotlib?
- How to make colorbar orientation horizontal in Python using Matplotlib?
- How to change the color of the ticks in the colorbar in Matplotlib?
- How to remove relative shift in Matplotlib axis?
- How to change the font properties of a Matplotlib colorbar label?
- How to add a colorbar for a hist2d plot in Matplotlib?
- How to plot a 2D matrix in Python with colorbar Matplotlib?
- How to plot a pcolor colorbar in a different subplot in Matplotlib?
- How to shift a graph along the X-axis in matplotlib?
- How to make a discrete colorbar for a scatter plot in matplotlib?
- How do I adjust (offset) the colorbar title in Matplotlib?
- Java Program to shift array elements to the right

Advertisements