- 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 make a scatter plot colored by density in Matplotlib?
We can create a dict for color and a value. If the same value comes up, we can use a scatter method and if the closer values have the same set of colors, that could make the plot color denser.
Steps
Create a new figure, or activate an existing figure.
Add an ~.axes.Axes to the figure as part of a subplot arrangement.
Get the x and y values using np.random.normal() method. Draw random samples from a normal (Gaussian) distribution.
Make a color list with red and blue colors.
To make it denser, we can store the same color with the same value.
Plot scatter point, a scatter plot of *y* vs. *x* with varying marker size and/or color.
Set the x-view limit.
Set the y-view limit.
To show the figure, use the plt.show() method.
Example
import random import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(1, 1, 1) x = np.random.normal(0.5, 0.3, 10000) y = np.random.normal(0.5, 0.3, 10000) colors = ['red', 'blue'] color = dict() for i in x: if i not in color: color[i] = colors[random.randint(1, 10) % len(colors)] ax.scatter(x, y, c=[color.get(i) for i in x]) ax.set_xlim(-0.5, 1.5) ax.set_ylim(-0.5, 1.5) plt.show()
Output
- Related Articles
- How to make a discrete colorbar for a scatter plot in matplotlib?
- How to animate a scatter plot in Matplotlib?
- How can I draw a scatter trend line using Matplotlib?
- How to make a 3D scatter plot in Python?
- Plot scatter points using plot method in Matplotlib
- How to plot a density map in Python Matplotlib?
- How can I plot a confusion matrix in matplotlib?
- How to make a scatter plot for clustering in Python?
- How can I make the xtick labels of a plot be simple drawings using Matplotlib?
- How can I plot hysteresis threshold in Matplotlib?
- How to plot a kernel density plot of dates in Pandas using Matplotlib?
- How can I plot a single point in Matplotlib Python?
- How to plot additional points on the top of a scatter plot in Matplotlib?
- How can Matplotlib be used to create three-dimensional scatter plot using Python?
- How can I convert from scatter size to data coordinates in Matplotlib?

Advertisements