Whitehot
Whitehot

Reputation: 497

Colour points in scatter plot based on density

I'm quite new to matplotlib, and I'm looking to make my scatterplots look a bit nicer. I have a lot of points to plot, so I'm looking into colouring them based on density for better readability.

I found this question which solved the problem... in 2013. Now when I try to run it, I get the error message ValueError: Using a string of single character colors as a color sequence is not supported. The colors can be passed as an explicit list instead.

How can I update this code to the current matplotlib release? Or is there something else that I'm missing causing the error? I'm using it in a Jupyter notebook, if that matters at all.

Here's the code I'm using:

# Generate fake data
x = np.random.normal(size=1000)
y = x * 3 + np.random.normal(size=1000)

# Calculate the point density
xy = np.vstack([x,y])
z = gaussian_kde(xy)(xy)

# Sort the points by density, so that the densest points are plotted last
idx = z.argsort()
x, y, z = x[idx], y[idx], z[idx]


fig, ax = plt.subplots()
ax.scatter(x, y, c=z, s=50, edgecolor='')
plt.show()

Upvotes: 2

Views: 7301

Answers (1)

luca
luca

Reputation: 7526

The API changes in new version of matplotlib, replace your code with:

ax.scatter(x, y, c=z, s=50, edgecolor=['none'])

Upvotes: 2

Related Questions