Errors with matplotlib plot, python

Обновить

February 2019

Просмотры

2.7k раз

1

Я получаю эту ужасную массивную ошибку при попытке построить с помощью 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.

Это код, я использую:

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()

Я не могу понять это на всех! Я думаю, что она работает и раньше, но потом просто перестал работать. Есть идеи?

4 ответы

1

Fix your identation?

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()

Got this:

enter image description here

2

Your problem is in this section:

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])

You're indexing a 2D array, and the result is still a 2D array. So, your Afreeze will get a 2D array appended, instead of a 1D array. Later, where you sum the separate elements of Afreeze, a summed 2D array will result in a 1D array, and that gets added to Sav and Svar. By the time you feed these variables to plt.plot(), matplotlib will get an array as one of the elements instead of a single number, which it of course can't cope with.

You probably want:

if any(t<0 for t in x): 
    p=np.where(x==min(x))
    Afreeze.append(A[p][0])

but I haven't tried to follow the logic of the script very much; that's up to you.

Perhaps good to see if this is indeed what you want: print the value of A[p][0] in the line before it gets appended to Afreeze.

I noted that because of the random.random() in the matrix creation, the if statement isn't always true, so the problem doesn't always show up. Minor detail, but could confuse people.

2

Your problem is in this section:

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])

You're indexing a 2D array, and the result is still a 2D array. So, your Afreeze will get a 2D array appended, instead of a 1D array. Later, where you sum the separate elements of Afreeze, a summed 2D array will result in a 1D array, and that gets added to Sav and Svar. By the time you feed these variables to plt.plot(), matplotlib will get an array as one of the elements instead of a single number, which it of course can't cope with.

You probably want:

if any(t<0 for t in x): 
    p=np.where(x==min(x))
    Afreeze.append(A[p][0])

but I haven't tried to follow the logic of the script very much; that's up to you.

Perhaps good to see if this is indeed what you want: print the value of A[p][0] in the line before it gets appended to Afreeze.

I noted that because of the random.random() in the matrix creation, the if statement isn't always true, so the problem doesn't always show up. Minor detail, but could confuse people.

2

Your problem is in this section:

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])

You're indexing a 2D array, and the result is still a 2D array. So, your Afreeze will get a 2D array appended, instead of a 1D array. Later, where you sum the separate elements of Afreeze, a summed 2D array will result in a 1D array, and that gets added to Sav and Svar. By the time you feed these variables to plt.plot(), matplotlib will get an array as one of the elements instead of a single number, which it of course can't cope with.

You probably want:

if any(t<0 for t in x): 
    p=np.where(x==min(x))
    Afreeze.append(A[p][0])

but I haven't tried to follow the logic of the script very much; that's up to you.

Perhaps good to see if this is indeed what you want: print the value of A[p][0] in the line before it gets appended to Afreeze.

I noted that because of the random.random() in the matrix creation, the if statement isn't always true, so the problem doesn't always show up. Minor detail, but could confuse people.