Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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
防止「;索引0超出大小为0的轴0的界限;Python中的错误_Python_Python 3.x_Numpy_Matplotlib - Fatal编程技术网

防止「;索引0超出大小为0的轴0的界限;Python中的错误

防止「;索引0超出大小为0的轴0的界限;Python中的错误,python,python-3.x,numpy,matplotlib,Python,Python 3.x,Numpy,Matplotlib,我有一个代码,在该代码中,我在函数图上得到了点的特定分布tan() 从底部和顶部通过直线进行限制: import matplotlib.pyplot as plt import numpy as np import sys import itertools import multiprocessing import tqdm ic = range(1,10) jc = range(1,10) paramlist = list(itertools.product(ic,jc)) def fu

我有一个代码,在该代码中,我在函数图上得到了点的特定分布
tan()
从底部和顶部通过直线进行限制:

import matplotlib.pyplot as plt
import numpy as np
import sys
import itertools
import multiprocessing
import tqdm

ic = range(1,10)
jc = range(1,10)

paramlist = list(itertools.product(ic,jc))

def func(params):
        ic = params[0]
        jc = params[1]

        fig = plt.figure(1, figsize=(10,6))

        x_all   = np.linspace(0, 10*np.pi, 10000, endpoint=False)
        x_above = x_all[ (-0.01)*ic*x_all < np.tan(x_all) ]
        x       = x_above[ np.tan(x_above) < 0.01*jc*x_above ]
        y       = np.tan(x)
        y2      = 0.01*jc*x
        y3      = (-0.01)*ic*x

        y_up   = np.diff(y) > 0
        y_diff = np.where( y_up, np.diff(y), 0 )
        x_diff = np.where( y_up, np.diff(x), 0 )
        diffs  = np.sqrt( x_diff**2 + y_diff**2 )
        length = diffs.sum()

        numbers = [2,4,6,8,10,12,14,16,18,20]

        p2 = []
        for d in range(len(numbers)):
            cumlenth = np.cumsum(diffs)
            s = np.abs(np.diff(np.sign(cumlenth-numbers[d]))).astype(bool)
            c = np.argwhere(s)[0][0]
            p = x[c], y[c]
            p2.append(p)

        p3 = sorted(p2, key=lambda x: x[0])
        x_max = p3[len(p3)-1][0]
        p4 = sorted(p2, key=lambda x: x[1])
        y_min = p4[0][1]
        y_max = p4[len(p3)-1][1]


        for b in range(len(p2)):
            plt.scatter( p2[b][0], p2[b][1], color="crimson", s=8)

        plt.plot(x, np.tan(x))
        plt.plot(x, y2)
        plt.plot(x, y3)

        ax = plt.gca()
        ax.set_xlim([0, x_max+0.5])
        ax.set_ylim([y_min-0.5, y_max+0.5]) 

        plt.savefig('C:\\Users\\tkp\\Desktop\\wykresy_4\\i='+str(ic)+'_j='+str(jc)+'.png', bbox_inches='tight')
        plt.show()

if __name__ == '__main__':

    p = multiprocessing.Pool(4)
    for params in tqdm.tqdm(p.imap_unordered(func, paramlist), total=len(paramlist)):
        #pass
        sys.stdout.write('\r'+ str(params))
        sys.stdout.flush()

    p.close()
    p.join()
导入matplotlib.pyplot作为plt
将numpy作为np导入
导入系统
进口itertools
导入多处理
导入TQM
ic=范围(1,10)
jc=范围(1,10)
paramlist=list(itertools.product(ic,jc))
def func(参数):
ic=参数[0]
jc=params[1]
图=plt.图(1,图尺寸=(10,6))
x_all=np.linspace(0,10*np.pi,10000,端点=False)
上面的x_=x_all[(-0.01)*ic*x_all0
y_diff=np.where(y_up,np.diff(y),0)
x_diff=np.where(y_up,np.diff(x),0)
差异=np.sqrt(x_差异**2+y_差异**2)
长度=差值和()
数字=[2,4,6,8,10,12,14,16,18,20]
p2=[]
对于范围内的d(长度(数字)):
cumlenth=np.累积和(差异)
s=np.abs(np.diff(np.sign(cumlenth number[d]))).astype(bool)
c=np.arg,其中[0][0]
p=x[c],y[c]
p2.追加(p)
p3=已排序(p2,键=λx:x[0])
x_max=p3[len(p3)-1][0]
p4=已排序(p2,key=lambda x:x[1])
y_min=p4[0][1]
y_max=p4[len(p3)-1][1]
对于范围内的b(len(p2)):
plt.散射(p2[b][0],p2[b][1],color=“crimson”,s=8)
平面图(x,np.tan(x))
平面图(x,y2)
plt.图(x,y3)
ax=plt.gca()
ax.set_xlim([0,x_max+0.5])
ax.set_ylim([y_min-0.5,y_max+0.5])
plt.savefig('C:\\Users\\tkp\\Desktop\\wykresy_4\\i='+str(ic)+''uj='+str(jc)+'.png',bbox_inches='tight')
plt.show()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
p=多处理池(4)
对于tqdm.tqdm中的参数(p.imap_无序(func,paramlist),total=len(paramlist)):
#通过
sys.stdout.write('\r'+str(参数))
sys.stdout.flush()
p、 关闭()
p、 加入
其中,例如,我接收到绘图:


问题是,如果我将
x_all=np.linspace(0,10*np.pi,10000,endpoint=False)中的范围设置得太小,
我得到的错误
索引0超出了大小为0的轴0的界限。我怎样才能保护你自己不受这种伤害?或者在这种情况下,我可以在“linspace”函数中设置一个变量范围

此错误发生在哪里?这是一个基本的信息-对我们来说,尤其是对你

@爱迪生说它在
argwhere
表达式中。我将尝试重新创建该步骤,首先猜测
diff
看起来像什么:

In [8]: x = np.ones(5)*.1                                                       
In [9]: x                                                                       
Out[9]: array([0.1, 0.1, 0.1, 0.1, 0.1])
In [10]: s = np.cumsum(x)                                                       
In [11]: s                                                                      
Out[11]: array([0.1, 0.2, 0.3, 0.4, 0.5])
In [12]: s-1                                                                    
Out[12]: array([-0.9, -0.8, -0.7, -0.6, -0.5])
In [13]: np.sign(s-1)                                                           
Out[13]: array([-1., -1., -1., -1., -1.])
In [14]: np.diff(np.sign(s-1))                                                  
Out[14]: array([0., 0., 0., 0.])
In [15]: np.abs(np.diff(np.sign(s-1)))                                          
Out[15]: array([0., 0., 0., 0.])
In [16]: np.abs(np.diff(np.sign(s-1))).astype(bool)                             
Out[16]: array([False, False, False, False])
不管到目前为止的细节如何,很可能
s
是一个只包含
False
的数组
其中
查找该数组中的
True
元素;没有

In [17]: np.where(_)                                                            
Out[17]: (array([], dtype=int64),)
arg其中
是此项的转置-每个维度一列,每个找到的项一行

In [18]: np.argwhere(_)                                                         
Out[18]: array([], shape=(0, 2), dtype=int64)
In [19]: _[0]                                                                   
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-19-aa79beb95eae> in <module>
----> 1 _[0]

IndexError: index 0 is out of bounds for axis 0 with size 0

您可以从那里向后操作,注意确保
差异
数字
正确,并始终找到有效的
c
。但是不管怎样,当使用
where
argwhere
时,请小心假设它已找到给定数量的项。

此错误发生在哪里?这是一个基本的信息-对我们来说,尤其是对你

@爱迪生说它在
argwhere
表达式中。我将尝试重新创建该步骤,首先猜测
diff
看起来像什么:

In [8]: x = np.ones(5)*.1                                                       
In [9]: x                                                                       
Out[9]: array([0.1, 0.1, 0.1, 0.1, 0.1])
In [10]: s = np.cumsum(x)                                                       
In [11]: s                                                                      
Out[11]: array([0.1, 0.2, 0.3, 0.4, 0.5])
In [12]: s-1                                                                    
Out[12]: array([-0.9, -0.8, -0.7, -0.6, -0.5])
In [13]: np.sign(s-1)                                                           
Out[13]: array([-1., -1., -1., -1., -1.])
In [14]: np.diff(np.sign(s-1))                                                  
Out[14]: array([0., 0., 0., 0.])
In [15]: np.abs(np.diff(np.sign(s-1)))                                          
Out[15]: array([0., 0., 0., 0.])
In [16]: np.abs(np.diff(np.sign(s-1))).astype(bool)                             
Out[16]: array([False, False, False, False])
不管到目前为止的细节如何,很可能
s
是一个只包含
False
的数组
其中
查找该数组中的
True
元素;没有

In [17]: np.where(_)                                                            
Out[17]: (array([], dtype=int64),)
arg其中
是此项的转置-每个维度一列,每个找到的项一行

In [18]: np.argwhere(_)                                                         
Out[18]: array([], shape=(0, 2), dtype=int64)
In [19]: _[0]                                                                   
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-19-aa79beb95eae> in <module>
----> 1 _[0]

IndexError: index 0 is out of bounds for axis 0 with size 0

您可以从那里向后操作,注意确保
差异
数字
正确,并始终找到有效的
c
。但不管怎样,当使用
where
argwhere
时,请小心假设它已找到给定数量的项。

您是否试图阻止程序自动关闭或希望能够重新键入它?@edison更好的方法是能够重新键入它。但这些情况可能并不重要,忽略它们可能就足够了。tqdm中参数的问题可能在
处。tqdm(p.imap_unordered(func,paramlist),total=len(paramlist)):
@edison是的,但它与
linspace
设置有关。因为如果我更改
stop=100*np.pi
则没有问题。执行
c=np.argwhere[0][0]
时会发生错误。“您是在试图阻止程序自动关闭还是希望能够重新键入它?@edison更好的方法是能够重新键入它。但这些情况可能并不重要,忽略它们可能就足够了。tqdm中参数的问题可能在
处。tqdm(p.imap_unordered(func,paramlist),total=len(paramlist)):
@edison是的,但它与
linspace
设置有关。因为如果我更改
stop=100*np.pi
则没有问题。执行
c=np.argwhere[0][0]
```