Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.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
matplotlib plot、python的错误_Python_Matplotlib - Fatal编程技术网

matplotlib plot、python的错误

matplotlib plot、python的错误,python,matplotlib,Python,Matplotlib,在尝试使用matplotlib绘图时,我遇到了一个可怕的巨大错误: Traceback (most recent call last): File "24oct_specanal.py", line 90, in <module> main() File "24oct_specanal.py", line 83, in main plt.plot(Svar,Sav) File "/usr/lib64/python2.6/site-packages/matp

在尝试使用matplotlib绘图时,我遇到了一个可怕的巨大错误:

Traceback (most recent call last):
  File "24oct_specanal.py", line 90, in <module>
    main()
  File "24oct_specanal.py", line 83, in main
    plt.plot(Svar,Sav)
  File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 2458, in plot
    ret = ax.plot(*args, **kwargs)
  File "/usr/lib64/python2.6/site-packages/matplotlib/axes.py", line 3849, in plot
    self.add_line(line)
  File "/usr/lib64/python2.6/site-packages/matplotlib/axes.py", line 1443, in add_line
    self._update_line_limits(line)
  File "/usr/lib64/python2.6/site-packages/matplotlib/axes.py", line 1451, in _update_line_limits
    p = line.get_path()
  File "/usr/lib64/python2.6/site-packages/matplotlib/lines.py", line 644, in get_path
    self.recache()
  File "/usr/lib64/python2.6/site-packages/matplotlib/lines.py", line 392, in recache
    x = np.asarray(xconv, np.float_)
  File "/usr/lib64/python2.6/site-packages/numpy/core/numeric.py", line 235, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
回溯(最近一次呼叫最后一次):
文件“24oct_specanal.py”,第90行,在
main()
文件“24oct_specanal.py”,第83行,主目录
plt.绘图(Svar、Sav)
文件“/usr/lib64/python2.6/site packages/matplotlib/pyplot.py”,绘图中第2458行
ret=最大绘图(*args,**kwargs)
文件“/usr/lib64/python2.6/site packages/matplotlib/axes.py”,第3849行,在绘图中
自我添加_行(行)
文件“/usr/lib64/python2.6/site packages/matplotlib/axes.py”,第1443行,添加行
自我更新行限制(行)
文件“/usr/lib64/python2.6/site packages/matplotlib/axes.py”,第1451行,在“更新”行中
p=line.get_path()
get_路径中的文件“/usr/lib64/python2.6/site packages/matplotlib/lines.py”,第644行
self.recache()
文件“/usr/lib64/python2.6/site packages/matplotlib/lines.py”,第392行,在recache中
x=np.asarray(xconv,np.float_ux)
asarray中的文件“/usr/lib64/python2.6/site packages/numpy/core/numeric.py”,第235行
返回数组(a,数据类型,copy=False,order=order)
ValueError:使用序列设置数组元素。
这是我正在使用的代码:

import numpy as np
import numpy.linalg
import random
import matplotlib.pyplot as plt
import pylab
from scipy.optimize import curve_fit
from array import array

def makeAImatrix(n):

    A=np.zeros((n,n))
    I=np.ones((n))
    for i in range(0,n):
        for j in range(i+1,n):
            A[j,i]=random.random()
    for i in range(0,n):
        for j in range(i+1,n):
                A[i,j] = A[j,i]
    for i in range(n):
        A[i,i]=1
    return (A, I)

def main():
    n=5 #number of species
    t=1 # number of matrices to check
    Aflat = []
    Aflatlist = [] #list of matrices
    Aflatav = []
    Aflatvar = []
    Aflatskew = []
    remspec = []
    Afreeze = [] #this is a LIST OF VECTORS that stores the vector corresponding to each extinct species as
                  #it is taken out. it is NOT the same as the original A matrix as it is only
                  #coherant in one direction. it is also NOT A SQUARE.
    Sex = [] # (Species extinct) this is a vector that corresponds to the Afreeze matrix. if a species is extinct then
                #the value stored here will be -1. 
    Sav = [] # (Species average) The average value of the A cooefficiants for each species
    Svar = [] # (Species variance)

    for k in range (0,t):
        allpos = 0
        A, I = makeAImatrix(n)
        while allpos !=1: #while all solutions are not positive

            x = numpy.linalg.solve(A,I)
            if any(t<0 for t in x): #if any of the solutions in x are negative
                p=np.where(x==min(x)) # find the most negative solution, p is the position

                #now store the A coefficiants of the extinct species in the Afreeze list
                Afreeze.append(A[p])
                Sex.append(-1) #given -1 value as species is extinct.

                x=np.delete(x, p, 0)
                A=np.delete(A, p, 0)
                A=np.delete(A, p, 1)
                I=np.delete(I, p, 0)

            else: 
                allpos = 1 #set allpos to one so loop is broken
        l=len(x)

        #now fill Afreeze and Sex with the remaining species that have survived
        for m in range (0, l):
            Afreeze.append(A[m])
            Sex.append(1) # value of 1 as this species has survived

        #now time to analyse the coefficiants for each species.

        for m in range (0, len(Sex)):
            X1 = sum(Afreeze[m])/len(Afreeze[m]) # this is the mean
            X2 = 0
            for p in range (len(Afreeze[m])):
                X2 = X2 + Afreeze[m][p]

            X2 = X2/len(Afreeze[m])
            Sav.append(X1)
            Svar.append(X2 - X1*X1)

    spec = []
    for b in range(0,n):
        spec.append(b)

    plt.plot(Svar,Sav)
    plt.show()
    #plt.scatter(spec, Sav)
    #plt.show()


if __name__ == '__main__':
    main()
将numpy导入为np
进口numpy.linalg
随机输入
将matplotlib.pyplot作为plt导入
进口派拉布
从scipy.optimize导入曲线\u拟合
从数组导入数组
def makeAImatrix(n):
A=np.零((n,n))
I=np.one((n))
对于范围(0,n)内的i:
对于范围(i+1,n)内的j:
A[j,i]=random.random()
对于范围(0,n)内的i:
对于范围(i+1,n)内的j:
A[i,j]=A[j,i]
对于范围(n)中的i:
A[i,i]=1
返回(A,I)
def main():
n=5#物种数量
t=1#要检查的矩阵数
Aflat=[]
Aflatlist=[]#矩阵列表
Aflatav=[]
Aflatvar=[]
Aflatskew=[]
remspec=[]
AFREZE=[]#这是一个向量列表,其中存储了每个灭绝物种对应的向量,如
#它被取出了。它与原始A矩阵不同,因为它只是
#在一个方向上凝聚。它也不是正方形。
性别=[]#(物种灭绝)这是一个对应于Afreeze矩阵的向量。如果一个物种灭绝了,那么
#此处存储的值将为-1。
Sav=[]#(物种平均值)每个物种的A协同效应的平均值
Svar=[]#(物种变异)
对于范围(0,t)内的k:
allpos=0
A、 I=makeAImatrix(n)
而所有的位置=1:#虽然并非所有解决方案都是积极的
x=numpy.linalg.solve(A,I)
如果有(t确定你的身份

import numpy as np
import numpy.linalg
import random
import matplotlib.pyplot as plt
import pylab
from scipy.optimize import curve_fit
from array import array

def main():
    n=20 #number of species
    spec=np.zeros((n+1))
    for i in range(0,n):
        spec[i]=i
    t=100 #initial number of matrices to check
    B = np.zeros((n+1)) #matrix to store the results of how big the matrices have to be

    for k in range (0,t):
        A=np.zeros((n,n))
        I=np.ones((n))
        for i in range(0,n):
            for j in range(i+1,n):
                A[j,i]=random.random()


        for i in range(0,n):
            for j in range(i+1,n):
                A[i,j] = A[j,i]


        for i in range(n):
            A[i,i]=1

        allpos = 0

        while allpos !=1: #while all solutions are not positive

            x = numpy.linalg.solve(A,I)
            if any(t<0 for t in x): #if any of the solutions in x are negative
                p=np.where(x==min(x)) # find the most negative solution, p is the position
                x=np.delete(x, p, 0)
                A=np.delete(A, p, 0)
                A=np.delete(A, p, 1)
                I=np.delete(I, p, 0)

            else: 
                allpos = 1 #set allpos to one so loop is broken
        l=len(x)
        B[l] = B[l]+1
    B = B/n
    pi=3.14

    resfile=open("results.txt","w")
    for i in range (0,len(spec)):
        resfile.write("%d " % spec[i])
        resfile.write("%0.6f \n" %B[i])
    resfile.close()


    plt.hist(B, bins=n)
    plt.title("Histogram")
    plt.show()

    plt.plot(spec,B)
    plt.xlabel("final number of species")
    plt.ylabel("fraction of total matrices")
    plt.title("plot")
    plt.show()
if __name__ == '__main__':
    main()
将numpy导入为np
进口numpy.linalg
随机输入
将matplotlib.pyplot作为plt导入
进口派拉布
从scipy.optimize导入曲线\u拟合
从数组导入数组
def main():
n=20#物种数量
规格=np.零((n+1))
对于范围(0,n)内的i:
规格[i]=i
t=100#要检查的矩阵的初始数量
B=np.zeros((n+1))#矩阵,用于存储矩阵大小的结果
对于范围(0,t)内的k:
A=np.零((n,n))
I=np.one((n))
对于范围(0,n)内的i:
对于范围(i+1,n)内的j:
A[j,i]=random.random()
对于范围(0,n)内的i:
对于范围(i+1,n)内的j:
A[i,j]=A[j,i]
对于范围(n)中的i:
A[i,i]=1
allpos=0
while allpos!=1:#当所有解都不是正解时
x=numpy.linalg.solve(A,I)

如果有任何问题(t您的问题在本节中:

if any(t<0 for t in x): #if any of the solutions in x are negative
    p=np.where(x==min(x)) # find the most negative solution, p is the position
    #now store the A coefficiants of the extinct species in the Afreeze list
    Afreeze.append(A[p])
但我并没有尽力去遵循剧本的逻辑,这取决于你

看看这是否真的是您想要的,也许是件好事:
print
A[p][0]的值,然后再将其附加到
Afreeze


我注意到这是因为
random.random()
在矩阵创建过程中,if语句并不总是正确的,因此问题并不总是出现。很小的细节,但可能会让人困惑。

非常抱歉…我之前发布了错误的代码,我发布了有效的代码。此新代码不起作用,因此任何输入都非常好。非常感谢您对其进行测试。第90行是版本最后一行写着main(),恐怕你的缩进还是很乱:
main()
比上面的
if
语句缩进得少。是的,很抱歉,这太糟糕了……现在应该可以了,已经修好了!非常感谢。
if any(t<0 for t in x): 
    p=np.where(x==min(x))
    Afreeze.append(A[p][0])