问题
I'm using Matplotlib and my goal is highlighting some points in a scatterplot.
I used the following code:
$colors = {'true':'red', 'false':'blue'}
plt.scatter(data[T[j]], data[T[i]], c=data['upgrade'].apply(lambda x: colors[x]) $
This code let my dots being red if the condition is "true", else blue. I haven't any problem until I had the following example:
20k dots and just 1 is TRUE. The plot that I obtained can't display my only point, because I have a cloud full of blue dots (2k) and just one should be red.
My question is if there is some way to show my only red dots, in general, to let red dots being more highlighted than the blue.
Thank you.
回答1:
You can take advantage of numpy's indexing arrays with array conditions. And then efficiently call scatter
twice. The ones you want on top you call last.
Additional tricks include using a less intense blue, and playing with the size of the dots. (Note that the dot size is relative to its area, not its diameter.)
import numpy as np
import matplotlib.pyplot as plt
N = 2000
x = np.random.rand(N)
y = np.random.rand(N)
z = np.random.rand(N)
c = np.where(z < 0.001, 1, 0)
plt.scatter(x[c==0], y[c==0], c='#2c7bb6', s=10)
plt.scatter(x[c==1], y[c==1], c='#ff0000', s=80)
plt.show()
来源:https://stackoverflow.com/questions/59090592/how-can-i-highlight-a-dot-in-a-cloud-of-dots-with-matplotlib