简体   繁体   English

Python散点图-如何查看每点的条目数

[英]Python scatter plot - how to see number of entries per point

I have data points that repeat themselves for quite a lot of entries (creating many overlapping (x,y) points) and I'm really interested in knowing the number of entries for each point on the graph. 我有重复很多数据条目的数据点(创建许多重叠的(x,y)点),并且我真的很想知道图中每个点的条目数。 Is there an easy way of doing this (besides the obvious of writing a piece of code that does this?) 有没有简单的方法可以做到这一点(除了显然可以编写一段代码来做到这一点之外?)

First of all get your points into a list of tuples: 首先,将您的观点归入元组列表:

L = [(1,2), (0,0), (0,0), (1,2), (1,2), (3,4)]

I'm assuming you're reading your data in from a file or something, and not hard-coding it like I did above, so modify your import routine to give you a list of tuples, or post-process your imported data to form your list of tuples. 我假设您正在从文件或其他内容中读取数据,并且不像上面一样对它进行硬编码,因此请修改导入例程以提供元组列表,或对导入的数据进行后处理以形成表格您的元组列表。

Why am I going on about tuples? 我为什么要继续学习元组? Because they are hashable, and therefore can be used to make a set: 由于它们是可散列的,因此可以用于建立集合:

S = set(L)
print (S)
set([(1, 2), (0, 0), (3, 4)])

Now we have all the unique points in the data, YAY! 现在,我们在数据中拥有所有独特的点,是的! ... but how many times is each repeated? ...但是每个重复多少次? That can be done by counting them in the list... Or being too lazy to do that get the list to count its-self using the lists count method: 可以通过在列表中对它们进行计数来完成...或太懒了以至于无法使用list count方法来获取列表以对其自身进行计数:

F = {}
for i in list(S):
    F[i] = L.count(i)

Now F is a dictionary containing a frequency table for each of our (X,Y) points F.keys() will give you all of the unique locations, and the dictionary contains how many times each point happened. 现在F是一个字典,其中包含我们每个(X,Y)点的频率表F.keys()将为您提供所有唯一的位置,并且该字典包含每个点发生了多少次。 Now all we need to do is plot it: 现在我们要做的就是绘制它:

from matplotlib.pyplot import figure, show
fig = figure()
sub = fig.add_subplot(111)

Due to the fact that we are using weird lists of tuples, we'll need to use some list comprehensions to get the data back into a format that plot likes: 由于我们使用的是元组的怪异列表,因此我们需要使用一些列表推导来将数据恢复为类似图的格式:

K = F.keys()
Xs = [i[0] for i in K]
Ys = [i[1] for i in K]

Now it will plot nicely: 现在它将很好地绘制:

sub.plot(Xs, Ys, 'bo')

and the plot can be annotated with our frequencies like so: 可以用我们的频率注释该图,如下所示:

for i in K:
    sub.annotate(F[i], xy=i)

Show the plot: 显示情节:

show()

And you will get something like this: 然后您将获得如下内容: 结果图

I'd recommend a Bubble Chart if the points aren't too close together. 如果两点之间的距离不太近,我建议您使用气泡图。 The number of overlapping points would be represented by the size of the bubble. 重叠点的数量将由气泡的大小表示。

You can do this in a spreadsheet (using Excel) or in Javascript (using Google Charts) . 您可以在电子表格(使用Excel)或Javascript (使用Google图表)中执行此操作

You could use multiple axis. 您可以使用多轴。
Check these Gallery examples for inspiration. 查看这些库示例以获取灵感。

From Matplotlib 1.4.2 documentation: 从Matplotlib 1.4.2文档中:

References: 参考文献:

multiple_yaxis_with_spines.py multiple_yaxis_with_spines.py

Matplotlib Examples Matplotlib示例

Source code: 源代码:

  1. plot_bmh.py plot_bmh.py
  2. two_scales.py two_scales.py
  3. multiple_yaxis_with_spines.py multiple_yaxis_with_spines.py

Examples: 例子:

plot_bmh.png two_scales.png multiple_yaxis_with_spines.png

You can also use colours to represent the number of occurrences of each scatter point. 您也可以使用颜色来表示每个散点的出现次数。 Note, that my code builds on the answer of @Mark. 请注意,我的代码基于@Mark的答案。

import numpy as np
import matplotlib as ml
import matplotlib.pyplot as plt

# generate some points
points = np.random.rand(2,200).round(decimals=1)*10

# count occurences
L = [tuple(ii) for ii in points.T]
S = set(L)
F = {}
for i in list(S):
    F[i] = L.count(i)

# make counts-array of same length as points and attribute colours
C = np.array([F[(x, y)] for x,y in zip(points[0], points[1])])
norm = ml.colors.Normalize(0, vmax=C.max())
colours = [ml.cm.ScalarMappable(norm=norm, cmap='Reds').to_rgba(c) for c in C]

# plot figure
fig, ax = plt.subplots(1,1)
p = ax.scatter(points[0], points[1], c=colours, edgecolor='k')

This is how the result may look like 这就是结果的样子

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

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