2

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()
Whitehot
  • 383
  • 3
  • 19
  • 1
    Just remove the `edgecolor=''` from `ax.scatter(x, y, c=z, s=50, edgecolor='')` and it will plot. – Jay Patel Mar 29 '21 at 14:02
  • 1
    You can use `edgecolor='none'` for a fully transparent edge. Or `linewidths=0`. Default the edgecolor is equal to the facecolor. With very small dots it can help to remove the edge, but for `s=50` there doesn't seem to be a lot of benefits. – JohanC Mar 29 '21 at 14:08
  • @JohanC if you type it into an answer, I'll accept it. I was too focused on the `c=z` part to even think of that – Whitehot Mar 29 '21 at 14:14

1 Answers1

2

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

ax.scatter(x, y, c=z, s=50, edgecolor=['none'])
tdy
  • 36,675
  • 19
  • 86
  • 83
luca
  • 7,178
  • 7
  • 41
  • 55