简体   繁体   English

散点图标记尺寸计算

[英]Scatter plot marker size calculation

I have a very specific problem. 我有一个非常具体的问题。 I have two numpy arrays and the corresponding element of each array represent a 2d point. 我有两个numpy数组,每个数组的相应元素代表一个2d点。

a = [1,2,1,6,1]

b = [5,0,3,1,5]

I want to plot a scatter plot where the size of the marker is based on how many times than point occurs. 我想绘制散点图,其中标记的大小基于点出现的次数。

That is : 那是 :

1,5 : 2

2,0 : 1

1,3 : 1

6,1 : 1

So the size array must be size = [2,1,1,1] and other two arrays can be 所以size数组必须是size = [2,1,1,1],其他两个数组都可以

a = [1,2,1,6] and b = [5,0,3,1] a = [1,2,1,6]b = [5,0,3,1]

So I must be able to call plt.scatter as follows: 所以我必须能够调用plt.scatter ,如下所示:

plt.scatter(a,b,s=size)

Since the question is tagged with numpy, we might use numpy. 由于问题标记为numpy,我们可能会使用numpy。 numpy.unique allows to calculate the counts of unique values of an array. numpy.unique允许计算数组唯一值的计数。

import numpy as np

a = [1,2,1,6,1]
b = [5,0,3,1,5]

u, c = np.unique(np.c_[a,b], return_counts=True, axis=0)

then 然后

# u=
[[1 3]
 [1 5]
 [2 0]
 [6 1]]
# c= 
[1 2 1 1]

This can be plotted like so, where an additional function may be used to normalize the counts to some point sizes for plotting 这可以像这样绘制,其中可以使用附加函数来将计数标准化为用于绘图的某些点大小

import matplotlib.pyplot as plt
s = lambda x : (((x-x.min())/float(x.max()-x.min())+1)*8)**2

plt.scatter(u[:,0],u[:,1],s=s(c))

plt.show()

在此输入图像描述

This will do what you want: 这将做你想要的:

from collections import Counter

a = [1, 2, 1, 6, 1]
b = [5, 0, 3, 1, 5]

counts = Counter([(x, y) for x, y in zip(a, b)])

size = [counts[(x, y)] for x, y in zip(a, b)]

counter will keep track of how many times each point appears in your arrays. counter将跟踪每个点在阵列中出现的次数。 Then size gets that number from counter . 然后大小从counter获取该数字。

Note that you actually want size = [2, 1, 1, 1, 2] because you need s to be the same size as your input arrays. 请注意,您实际上需要size = [2, 1, 1, 1, 2]因为您需要s与输入数组的大小相同。 This won't matter though; 但这并不重要; you'll just plot the same point twice. 你只需要两次绘制相同的点。

If you really do want to remove the duplicates, you could do the same thing, but add an extra step, where you create a set of points. 如果你真的想删除重复项,你可以做同样的事情,但添加一个额外的步骤,你创建一set点。

from collections import Counter

a = [1, 2, 1, 6, 1]
b = [5, 0, 3, 1, 5]

counts = Counter([(x, y) for x, y in zip(a, b)])

points = set([(x, y) for x, y in zip(a, b)])
a = list()
b = list()
for x, y in points:
    a.append(x)
    b.append(y)

size = [counts[(x, y)] for x, y in zip(a, b)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM