python - Animated Line Graph gets ValueError: x and y must have same first dimension at runtime -
i trying create animated line graph of half lives, text files referenced this:
decay1.txt
:
2000 1000 500 250 125 63 31 16 8 4 0
decay2.txt
reverse of decay1.txt
.
any appreciated, believe how pulling data arrays create y axis. data enter when prompted 2000 10.
import numpy np import matplotlib.pyplot plt import matplotlib.animation 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 total time 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
error print out:
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()" more information. >>> ================================ restart ================================ >>> enter total number of nuclei2000 enter total time 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 line in self._get_lines(*args, **kwargs): file "c:\python33\lib\site-packages\matplotlib\axes\_base.py", line 304, in _grab_next_args 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 , y must have same first dimension") valueerror: x , y must have same first dimension >>>
Comments
Post a Comment