Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将散点图指定给特定存储箱时出错_Python_Pandas_Numpy_Dataframe_Matplotlib - Fatal编程技术网

Python 将散点图指定给特定存储箱时出错

Python 将散点图指定给特定存储箱时出错,python,pandas,numpy,dataframe,matplotlib,Python,Pandas,Numpy,Dataframe,Matplotlib,我正在尝试对特定装箱区域内的分散点的数量进行计数。code在我使用XY数据的单个行时起作用,但当我尝试迭代相同的脚本过多的行时返回类型错误: TypeError: only length-1 arrays can be converted to Python scalars 例如: import math import matplotlib.pyplot as plt import matplotlib as mpl import numpy as np np.random.seed(42)

我正在尝试对特定装箱区域内的
分散点的数量进行
计数。
code
在我使用
XY
数据的单个
时起作用,但当我尝试
迭代
相同的
脚本
过多的
行时
返回
类型错误

TypeError: only length-1 arrays can be converted to Python scalars
例如:

import math
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

np.random.seed(42)
X = np.random.randint(-80, 80, size=(100, 10))
Y = np.random.randint(0, 120, size=(100, 10))  

fig, ax = plt.subplots()

BIN_23_X = 0 
ang1 = -60, 60
ang2 = 60, 60
angle = math.degrees(math.acos(2/9.15))
E_xy = 0,60

Halfway = mpl.lines.Line2D((BIN_23_X,BIN_23_X), (0,125), color = 'white', lw = 1.5, alpha = 0.8, zorder = 1)
arc1 = mpl.patches.Arc(ang1, 70, 110, angle = 0, theta2 = angle, theta1 = 360-angle, color = 'white', lw = 2)
arc2 = mpl.patches.Arc(ang2, 70, 110, angle = 0, theta2 = 180+angle, theta1 = 180-angle, color = 'white', lw = 2)
Oval = mpl.patches.Ellipse(E_xy, 160, 130, lw = 3, edgecolor = 'black', color = 'white', alpha = 0.2)

ax.add_line(Halfway)
ax.add_patch(arc1)
ax.add_patch(arc2)
ax.add_patch(Oval)

ov1 = mpl.patches.Ellipse(ang1, 70, 110, alpha=0)
ov2 = mpl.patches.Ellipse(ang2, 70, 110, alpha=0)
ax.add_patch(ov1)
ax.add_patch(ov2)

for px, py in zip(X, Y):
    #Error occurs in the line below
    in_oval = Oval.contains_point(ax.transData.transform(([px, py])), 0)
    in_left = ov1.contains_point(ax.transData.transform(([px, py])), 0)
    in_right = ov2.contains_point(ax.transData.transform(([px, py])), 0)
    on_left = px < 0
    on_right = px > 0
    if in_oval:
        if in_left:
            n_bin = 1
        elif in_right:
            n_bin = 4
        elif on_left:
            n_bin = 2
        elif on_right:
            n_bin = 3
        else:
            n_bin = -1
    else:
        n_bin = -1

def bin_counts(xA, yA):
    bc = dict()
    E = Oval.contains_points(ax.transData.transform(np.array([xA, yA]).T), 0)
    E_l = ov1.contains_points(ax.transData.transform(np.array([xA, yA]).T), 0)
    E_r = ov2.contains_points(ax.transData.transform(np.array([xA, yA]).T), 0)
    L = np.array(xA) < 0
    R = np.array(xA) > 0
    bc[1] = np.sum(E & E_l)
    bc[2] = np.sum(E & L & ~E_l)
    bc[3] = np.sum(E & R & ~E_r)
    bc[4] = np.sum(E & E_r)
    return bc        

for xr, yr in zip(X, Y):
    print(bin_counts(xr, yr))
回溯:

Studies/Datasets/codes/untitled4.py", line 73, in <module>
    in_oval = Oval.contains_point(ax.transData.transform(([px, py])), 0)

  File "/Users/jeremyalexander/anaconda/lib/python3.6/site-packages/matplotlib/patches.py", line 154, in contains_point
radius)

  File "/Users/jeremyalexander/anaconda/lib/python3.6/site-packages/matplotlib/path.py", line 493, in contains_point
return _path.point_in_path(point[0], point[1], radius, self, transform)

TypeError: only length-1 arrays can be converted to Python scalars
Studies/dataset/code/untitled4.py”,第73行
in_oval=oval.包含_点(ax.transData.transform([px,py]),0)
文件“/Users/jeremyalexander/anaconda/lib/python3.6/site packages/matplotlib/patches.py”,第154行,包含
半径)
文件“/Users/jeremyalexander/anaconda/lib/python3.6/site packages/matplotlib/path.py”,第493行,包含点
返回路径。路径中的点(点[0],点[1],半径,自,变换)
TypeError:只有长度为1的数组才能转换为Python标量

我复制粘贴了你的代码,并得到了这个回溯-完整的回溯,而不仅仅是最后两行

Traceback (most recent call last):
  File "stack52695533.py", line 35, in <module>
    in_oval = Oval.contains_point(ax.transData.transform(([px, py])), 0)
  File "/usr/local/lib/python3.6/dist-packages/matplotlib/patches.py", line 154, in contains_point
    radius)
  File "/usr/local/lib/python3.6/dist-packages/matplotlib/path.py", line 493, in contains_point
    return _path.point_in_path(point[0], point[1], radius, self, transform)
TypeError: only size-1 arrays can be converted to Python scalars
就在问题行之前,生成:

[ 22  12 -66  26  -9 -60  22  41  -6   7] [111  67   4 119  36  71 105 112  91  30]
[[ 10992.    4488. ]
 [-32656.    9662.4]
 [ -4384.  -22123.2]
 [ 10992.   15206.4]
 [ -2896.    2640. ]
 [ 55136.   24816. ]
 [  2064.   44035.2]
 [ 17936.   26294.4]
 [ 52160.   41448. ]
 [ 45216.   11140.8]]
正如我所怀疑的,您正在尝试测试多个点,而不是一次测试一个点

X
Y
是(100,10);在第一个维度上迭代,但仍然将10个值传递给
transform


幸运的是,你提供了一个可运行的例子,否则我仍然在等待你对我最后一条评论的回应。

请考虑简化你的代码,这样人们可以更容易地理解它并帮助你。@ EdgRr.Mang拖拽,我已经拿出了一些绘图代码,但是剩下的是问题的关键。也许试着显示2个代码块:工作的代码:…“和不工作的代码:…”。。。“您提供给函数的其中一个参数具有它可以接受的更多值。它需要一个标量或可以转换为标量的东西。回顾相关函数调用的要求,回顾输入的类型和形状。你知道什么是回溯吗?或者如何准确地告诉我们错误发生在代码中的什么位置?抱歉。由于代码使用一行XY数据,它可以使用多行还是必须重写?一个起点是研究
包含的\u点
代码,看看它是否可以适应同时使用多个点。我没那么做,所以帮不了你。别担心。谢谢你的帮助。
print(px, py)
print(ax.transData.transform(([px, py])))
[ 22  12 -66  26  -9 -60  22  41  -6   7] [111  67   4 119  36  71 105 112  91  30]
[[ 10992.    4488. ]
 [-32656.    9662.4]
 [ -4384.  -22123.2]
 [ 10992.   15206.4]
 [ -2896.    2640. ]
 [ 55136.   24816. ]
 [  2064.   44035.2]
 [ 17936.   26294.4]
 [ 52160.   41448. ]
 [ 45216.   11140.8]]