[英]How to have scatter points become larger for higher density using matplotlib?
I am using matplotlib to draw a plot. 我正在使用matplotlib绘制图。 The x and y axises are discrete values and therefore, several (x,y) points in the list have the same values. x和y轴是离散值,因此列表中的几个(x,y)点具有相同的值。 For example, assume we have the following x,y points: 例如,假设我们有以下x,y点:
x=[0,1,1,2,2,2,2,2]
y=[0,1,2,3,3,3,3,5]
Now all of the (x,y) points occur once, but (2,3) occurs 4 times. 现在所有(x,y)点出现一次,但是(2,3)点出现4次。 When I use plt.scatter(x,y) , it only shows (2,3) as a regular point, once. 当我使用plt.scatter(x,y)时 ,它一次仅将(2,3)显示为常规点。 How is it possible to show the density of occurrence, too? 如何也可以显示发生的密度? For example, a bigger marker? 例如,更大的标记?
You can pass an optional argument s
to plt.scatter
that indicates the size of each point being plotted. 您可以将可选参数s
传递给plt.scatter
,该参数指示要绘制的每个点的大小。 To get the size to correspond with the occurrences of each point, first construct a dictionary that count the occurrences, then create a list of the sizes for each point. 要使大小与每个点的出现次数相对应,请首先构造一个对出现次数进行计数的字典,然后创建每个点的大小列表。
import matplotlib.pyplot as plt
from collections import Counter
x=[0,1,1,2,2,2,2,2]
y=[0,1,2,3,3,3,3,5]
# count the occurrences of each point
c = Counter(zip(x,y))
# create a list of the sizes, here multiplied by 10 for scale
s = [10*c[(xx,yy)] for xx,yy in zip(x,y)]
# plot it
plt.scatter(x, y, s=s)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.