How can I highlight a dot in a cloud of dots with Matplotlib?

蓝咒 提交于 2020-02-06 08:24:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!