- 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 can I convert numbers to a color scale in Matplotlib?
To convert numbers to a color scale in matplotlib, we can take the following steps.
Steps
- Create x, y and c data points using numpy.
- Convert the data points to Pandas dataframe.
- Create a new figure or activate an existing figure using subplots() method.
- Get the hot colormap.
- To linearly normalize the data, we can use Normalize() class.
- Plot the scatter points with x and y data points and linearly normalized colormap.
- Set the xticks for x data points.
- To make the colorbar, create a scalar mappable object.
- Use colorbar() method to make the colorbar.
- To display the figure, use show() method.
Example
from matplotlib import pyplot as plt, colors import numpy as np import pandas as pd plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(12) y = np.random.rand(len(x)) * 20 c = np.random.rand(len(x)) * 3 + 1.5 df = pd.DataFrame({"x": x, "y": y, "c": c}) fig, ax = plt.subplots() cmap = plt.cm.hot norm = colors.Normalize(vmin=2.0, vmax=5.0) ax.scatter(df.x, df.y, color=cmap(norm(df.c.values))) ax.set_xticks(df.x) sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm) fig.colorbar(sm) plt.show()
Output
- Related Articles
- How to plot contourf and log color scale in Matplotlib?
- How do I convert (or scale) axis values and redefine the tick frequency in Matplotlib?
- How can I get the color of the last figure in Matplotlib?
- How can I plot NaN values as a special color with imshow in Matplotlib?
- How can I convert from scatter size to data coordinates in Matplotlib?
- How do I set color to Rectangle in Matplotlib?
- How to convert data values into color information for Matplotlib?
- How can I set the background color on specific areas of a Pyplot figure using Matplotlib?
- How to insert a scale bar in a map in Matplotlib?
- How can I convert a string to boolean in JavaScript?
- How can I plot a confusion matrix in matplotlib?
- How do I change the font size of the scale in Matplotlib plots?
- How can I convert bytes to a Python string?
- How can I convert a Python tuple to string?
- How to fill color below a curve in Matplotlib?

Advertisements