使用matplotlib,如何使散点变得更大以实现更高的密度? [英] How to have scatter points become larger for higher density using matplotlib?

查看:72
本文介绍了使用matplotlib,如何使散点变得更大以实现更高的密度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib绘制图. x和y轴是离散值,因此列表中的几个(x,y)点具有相同的值.例如,假设我们有以下x,y点:

I am using matplotlib to draw a plot. The x and y axises are discrete values and therefore, several (x,y) points in the list have the same values. For example, assume we have the following x,y points:

x=[0,1,1,2,2,2,2,2]
y=[0,1,2,3,3,3,3,5]

现在所有(x,y)点出现一次,但是(2,3)点出现4次.当我使用 plt.scatter(x,y)时,它一次仅将(2,3)显示为常规点.如何也可以显示发生的密度?例如,更大的标记?

Now all of the (x,y) points occur once, but (2,3) occurs 4 times. When I use plt.scatter(x,y), it only shows (2,3) as a regular point, once. How is it possible to show the density of occurrence, too? For example, a bigger marker?

推荐答案

您可以将可选参数s传递给plt.scatter,该参数指示要绘制的每个点的大小.要使大小与每个点的出现次数相对应,请首先构造一个字典以对出现的次数进行计数,然后创建每个点的大小列表.

You can pass an optional argument s to plt.scatter that indicates the size of each point being plotted. 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()

这篇关于使用matplotlib,如何使散点变得更大以实现更高的密度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆