gpt4 book ai didi

python - 如何使用 matplotlib 让散点变得更大以获得更高的密度?

转载 作者:行者123 更新时间:2023-12-04 14:31:25 24 4
gpt4 key购买 nike

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

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) 作为一个常规点,一次。如何也可以显示发生的密度?例如,更大的标记?

最佳答案

您可以传递一个可选参数 splt.scatter表示绘制的每个点的大小。要获得与每个点的出现次数相对应的大小,首先构建一个计算出现次数的字典,然后为每个点创建一个大小列表。

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()

enter image description here

关于python - 如何使用 matplotlib 让散点变得更大以获得更高的密度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46700733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com