【发布时间】:2018-11-28 10:22:39
【问题描述】:
我有一个scatter plot,它被分类为bins。有 4 个 bin,中间用一条线隔开的两条弧线。它逐行排序到列表列表中。例如。如果每个 bin 中有一个散点,则导出为:
x[0],y[0] = [(x,y)],[(x,y)],[(x,y)],[(x,y)]
问题是我必须手动导出每一行。因此,如果我想导出散点图的第二行,我将更改为 x[1],y[1] 并将其添加到第一行。如果我有多行,这不是很有效。
如果我使用x,y,我会得到一个值错误:ValueError: operands could not be broadcast together with shapes (70,) (10,)
有没有一种方法可以逐行导出整个数据集,或者使用相同的代码并循环遍历每一行。
import math
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.random.randint(80, size=(400, 10))
y = np.random.randint(80, size=(400, 10))
fig, ax = plt.subplots()
ax.grid(False)
plt.scatter(x[0],y[0])
#Creating the arcs
BIN_23_X = 50
ang1 = 0, 50
ang2 = 100, 50
angle = math.degrees(math.acos(5.5/9.15))
#Adding the arcs and halfway line
Halfway = mpl.lines.Line2D((BIN_23_X,BIN_23_X), (0,100), c = 'black', lw = 2.5, alpha = 0.8, zorder = 1)
arc1 = mpl.patches.Arc(ang1, 65, 100, angle = 0, theta2 = angle, theta1 = 360-angle, lw = 2)
arc2 = mpl.patches.Arc(ang2, 65, 100, angle = 0, theta2 = 180+angle, theta1 = 180-angle, lw = 2)
ax.add_line(Halfway)
ax.add_patch(arc1)
ax.add_patch(arc2)
#Sorting the coordinates into bins
def get_nearest_arc_vert(x, y, arc_vertices):
err = (arc_vertices[:,0] - x)**2 + (arc_vertices[:,1] - y)**2
nearest = (arc_vertices[err == min(err)])[0]
return nearest
arc1v = ax.transData.inverted().transform(arc1.get_verts())
arc2v = ax.transData.inverted().transform(arc2.get_verts())
def classify_pointset(vx, vy):
bins = {(k+1):[] for k in range(4)}
for (x,y) in zip(vx, vy):
nx1, ny1 = get_nearest_arc_vert(x, y, arc1v)
nx2, ny2 = get_nearest_arc_vert(x, y, arc2v)
if x < nx1:
bins[1].append((x,y))
elif x > nx2:
bins[4].append((x,y))
else:
if x < BIN_23_X:
bins[2].append((x,y))
else:
bins[3].append((x,y))
return bins
#Bins Output
bins_red = classify_pointset(x[0], y[0])
all_points = [None] * 5
for bin_key in [1,2,3,4]:
all_points[bin_key] = bins_red[bin_key]
print(all_points)
我要分类的行是:
bins = classify_pointset(x[0], y[0])
我可以更改bins = classify_pointset(x[0], y[0]) 或添加一个循环来遍历每一行吗?
我希望完成的示例
如果我们使用数据的第一行来返回我会使用的分箱坐标:
bins = classify_pointset(x[0], y[0])
输出:
[None, [(17, 20), (20, 36), (23, 30), (0, 65), (15, 35)], [(44, 57), (45, 3), (43, 0)], [(61, 21)], [(78, 23)]]
如您所见,第一个 bin [(17, 20), (20, 36), (23, 30), (0, 65), (15, 35)] 中有 5 个坐标。第 2 个中 3 个[(44, 57), (45, 3), (43, 0)],第 3 个中 1 个[(61, 21)],第 4 个中 1 个[(78, 23)]
要返回第二行分箱坐标,我会更改:
bins = classify_pointset(x[0], y[0]) 到 bins = classify_pointset(x[1], y[1])。
然后我会将第二行附加到第一行以创建:
0 = [(x,y)],[(x,y)],[(x,y)],[(x,y)]
1 = [(x,y)],[(x,y)],[(x,y)],[(x,y)]
这个问题是我必须继续手动更改行并追加。例如
返回bins = classify_pointset(x[2], y[2]),然后追加:
输出:
2 = [(x,y)],[(x,y)],[(x,y)],[(x,y)]
追加:
0 = [(x,y)],[(x,y)],[(x,y)],[(x,y)]
1 = [(x,y)],[(x,y)],[(x,y)],[(x,y)]
2 = [(x,y)],[(x,y)],[(x,y)],[(x,y)]
我需要将整个 xy 数据集以逐行格式返回到它们各自的 bin 中的东西。而不是一次导出一行然后追加。
这有意义吗?
【问题讨论】:
-
#Coordinates get sorted into bins to produce a dict你的问题是如果没有dict,我们如何装箱?在这种情况下,你的分箱逻辑是什么? -
无论你用什么来分箱,你能分箱到一个字典列表中,即用
d_1strow代替d_bins[0]等吗?然后遍历该列表。 -
@jpp 我已将其更改为列表列表并添加了分箱方法
-
@tobias_k,这会出现在
if声明中还是之后会出现? -
我将对此进行研究,但您能否举一个输入示例以及相应的输出是什么?
标签: python pandas loops numpy append