Question

I want to generate a scatter graph using matlibplot.pyplot and a lot of points are duplicate. So when I simply draw a graph use pyplot.scatter method, many points are overdrawn on one dot. In order to consider the number of points on that position, I think I need to set a different point size, like a bigger circle if the spot contains more data points.

Can someone give me a pointer on how to do this? Thanks!

Was it helpful?

Solution

The easiest way to do this is to set a low alpha, and then when the points plot on top of each other they look darker.

import numpy as np
import matplotlib.pyplot as plt

data = [i for i in range(8) for j in range(np.random.randint(10))]
x, y = np.array(data), np.array(data)
plt.scatter(x, y, alpha=.1, s=400)
plt.show()

enter image description here

Of course, you can also change the size of the point or the color directly. To do this, you need to find the number of overlapping points and then set the size (using the s scatter plot parameter) or the color (using c) or both. (But setting alpha is easiest since it doesn't require explicitly counting the overlaps.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top