动画线图获取ValueError:x和y在运行时必须具有相同的第一个维度

时间:2015-11-01 13:53:18

标签: python matplotlib graph runtime-error axis

我正在尝试创建半生命的动画线图,引用的文本文件如下所示:

decay1.txt

2000
1000
500
250
125
63
31
16
8
4
0

decay2.txtdecay1.txt相反。

任何帮助都会受到赞赏,我相信很可能与我如何从数组中提取数据以创建y轴有关。我提示时输入的数据是2000然后是10。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
totalnumber_of_nuclei = int(input("Enter total number of nuclei"))#user input totalnumber of nuclei(max y)
TotalTime = int(input("Enter the total time for simulation"))#user input totaltime (max x)
Timescale = np.arange(0., (TotalTime+1.) ,1.)#Time between plots - distance on x between plots
ax = plt.axes(xlim=(0, TotalTime), ylim=(0, totalnumber_of_nuclei))

def animate(i):
    pullData1 = open("decay1.txt","r").read()
    pullData2 = open("decay2.txt","r").read()
    dataArray1 = pullData1.split("\n")
    dataArray2 = pullData2.split("\n")
    print(int(dataArray1[i]),int(dataArray2[i]))
    y1 = []
    y2 = []
    x = Timescale
    if len(dataArray1[i])>1:
        y1.append(int(dataArray1[i]))
    if len(dataArray2[i])>1:
        y2.append(int(dataArray2[i]))
    ax.plot(x, y1, "b-", marker="o", label="decay1")
    ax.plot(x, y2, "r-", marker="o", label="decay2")

ax.legend(loc='upper left', frameon=False)
plt.ylabel("Number of Nuclei")#label y axis Number of Nuclei
plt.xlabel("Time")#label x axis Time
ani = animation.FuncAnimation(fig, animate, interval=1000, blit=True)
plt.show()#display graph

打印错误:

Python 3.3.5 (v3.3.5:62cf4e77f785, Mar  9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Enter total number of nuclei2000
Enter the total time for simulation10

Warning (from warnings module):
  File "C:\Python33\lib\site-packages\matplotlib\axes\_axes.py", line 475
    warnings.warn("No labelled objects found. "
UserWarning: No labelled objects found. Use label='...' kwarg on individual plots.
2000 0
Traceback (most recent call last):
  File "C:\Users\Ruari\Documents\School Work\Dropbox\comp 4\tutorial.py", line 30, in <module>
    ani = animation.FuncAnimation(fig, animate, interval=1000, blit=True)
  File "C:\Python33\lib\site-packages\matplotlib\animation.py", line 1067, in __init__
    TimedAnimation.__init__(self, fig, **kwargs)
  File "C:\Python33\lib\site-packages\matplotlib\animation.py", line 913, in __init__
    *args, **kwargs)
  File "C:\Python33\lib\site-packages\matplotlib\animation.py", line 591, in __init__
    self._init_draw()
  File "C:\Python33\lib\site-packages\matplotlib\animation.py", line 1092, in _init_draw
    self._draw_frame(next(self.new_frame_seq()))
  File "C:\Python33\lib\site-packages\matplotlib\animation.py", line 1106, in _draw_frame
    self._drawn_artists = self._func(framedata, *self._args)
  File "C:\Users\Ruari\Documents\School Work\Dropbox\comp 4\tutorial.py", line 24, in animate
    ax.plot(x, y1, "b-", marker="o", label="decay1")
  File "C:\Python33\lib\site-packages\matplotlib\axes\_axes.py", line 1373, in plot
    for line in self._get_lines(*args, **kwargs):
  File "C:\Python33\lib\site-packages\matplotlib\axes\_base.py", line 304, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Python33\lib\site-packages\matplotlib\axes\_base.py", line 282, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "C:\Python33\lib\site-packages\matplotlib\axes\_base.py", line 223, in _xy_from_xy
    raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension
>>> 

0 个答案:

没有答案