- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 make a discrete colorbar for a scatter plot in matplotlib?
Using plt.colorbar(ticks=np.linspace(-2, 2, 5)), we can create a discrete color bar.
Steps
Return random floats in the half open interval, i.e., x, using np.random.random method.
Return random floats in the half open interval, i.e., y, using np.random.random method.
Return random integers from `low` (inclusive) to `high` (exclusive), i.e., z, using np.random.randint(-2, 3, 20) method.
Set the X-axis label using plt.xlabel().
Set the Y-axis label using plt.ylabel().
Use the built-in rainbow colormap.
Generate a colormap index based on discrete intervals.
A scatter plot of *y* vs. *x* with varying marker size and/or color, with x, y and z are created (Steps 1, 2, 3).
Create a colorbar for a ScalarMappable instance, *mappable*.
Use plt.show() to show the figure.
Example
from matplotlib import pyplot as plt import numpy as np from matplotlib import colors x = np.random.random(20) y = np.random.random(20) z = np.random.randint(-2, 3, 20) plt.xlabel('X-axis ') plt.ylabel('Y-axis ') cmap = plt.cm.rainbow norm = colors.BoundaryNorm(np.arange(-2.5, 3, 1), cmap.N) plt.scatter(x, y, c=z, cmap=cmap, norm=norm, s=100, edgecolor='none') plt.colorbar(ticks=np.linspace(-2, 2, 5)) plt.show()
Output
- Related Articles
- How to plot scatter points in a 3D figure with a colorbar in Matplotlib?
- How to add a colorbar for a hist2d plot in Matplotlib?
- How to make a scatter plot for clustering in Python?
- How to animate a scatter plot in Matplotlib?
- How can I make a scatter plot colored by density in Matplotlib?
- How to draw an average line for a scatter plot in MatPlotLib?
- How to make a 3D scatter plot in Python?
- How to plot a pcolor colorbar in a different subplot in Matplotlib?
- How to plot a 2D matrix in Python with colorbar Matplotlib?
- How to use different markers for different points in a Pylab scatter plot(Matplotlib)?
- How to plot additional points on the top of a scatter plot in Matplotlib?
- How to make a rug plot in Matplotlib?
- How to make a mosaic plot in Matplotlib?
- Adding caption below X-axis for a scatter plot using Matplotlib
- How to make colorbar orientation horizontal in Python using Matplotlib?
