matplotlib plot错误,python

2024-10-01 17:24:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试使用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.

这是我使用的代码:

^{pr2}$

我根本搞不懂!我想它以前是有用的,但后来就停止工作了。有什么想法吗?在


Tags: inpyselfaddplotmatplotlibmainpackages
2条回答

修正你的身份?在

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

明白了:

enter image description here

你的问题在这个部分:

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

您正在索引一个2D数组,结果仍然是一个2D数组。因此,Afreeze将追加一个2D数组,而不是1D数组。稍后,在对Afreeze的单独元素求和时,求和的2D数组将生成一个1D数组,并将其添加到Sav和{}。当您将这些变量输入plt.plot()时,matplotlib将获得一个数组作为元素之一,而不是一个它当然无法处理的单个数字。在

你可能想要:

^{pr2}$

但我并没有太多地遵循剧本的逻辑,这取决于你。在

看看这是否真的是你想要的,也许很好:print行中的一个[p][0]的值,它被附加到Afreeze之前。在

我注意到,由于矩阵创建中的random.random(),if语句并不总是正确的,所以问题并不总是出现。小细节,但可能会让人迷惑。在

相关问题 更多 >

    热门问题