有人可以帮我解决Python 3.7索引超出范围错误的问题吗? (我到处都看过)

时间:2019-04-05 19:55:41

标签: python python-3.x loops indexoutofrangeexception

评论中的答案---谢谢! 代码粘贴在下面。这是典型的“ 一次关闭”错误,但我无法弄清楚在哪里,原因或方式。诚然,就数组和索引而言,我实在很垃圾。如果有人可以帮我解决这个问题,我将不胜感激。

所以,我遇到的第一个错误是相同的,但它的目标是arrow数组。您可以在下面的代码中看到我的评论,这些代码在哪里进行了更改以及如何进行了更改。现在,它进入下一个数组,这是从其他两个数组创建的 ndnumpy 数组,并且像我第一次一样,它不允许我“附加”它来修复它。抛出属性错误,表示ndnumpy数组无法添加。

想补充一点,动画将打开并运行一会儿,然后停止并抛出错误。因此,从技术上讲,它有效,只是总是以错误结尾。

真的不需要if循环,它运行完全相同或完全没有......

这是Python IDLE引发的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 749, in callit
    func(*args)
  File "C:\Program Files\Python37\lib\site-packages\matplotlib\backends\_backend_tk.py", line 126, in _on_timer
    TimerBase._on_timer(self)
  File "C:\Program Files\Python37\lib\site-packages\matplotlib\backend_bases.py", line 1238, in _on_timer
    ret = func(*args, **kwargs)
  File "C:\Program Files\Python37\lib\site-packages\matplotlib\animation.py", line 1460, in _step
    still_going = Animation._step(self, *args)
  File "C:\Program Files\Python37\lib\site-packages\matplotlib\animation.py", line 1191, in _step
    self._draw_next_frame(framedata, self._blit)
  File "C:\Program Files\Python37\lib\site-packages\matplotlib\animation.py", line 1210, in _draw_next_frame
    self._draw_frame(framedata)
  File "C:\Program Files\Python37\lib\site-packages\matplotlib\animation.py", line 1762, in _draw_frame
    self._drawn_artists = self._func(framedata, *self._args)
  File "C:\Program Files\Python37\animationChegg\animate.py", line 64, in update
    arrow = ax.arrow(x[0,initiate], y[0,initiate],x[0,terminate] - x[0,initiate], y[0,terminate] - y[0,initiate],color = col,head_width=0.55)
IndexError: index 295 is out of bounds for axis 1 with size 295
>>> 
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation

x_coor = pd.read_csv('x_fiji.csv',header=None)
y_coor = pd.read_csv('y_fiji.csv',header=None)
waves = pd.read_csv('waves.csv',header=None)
initiation_pts = pd.read_csv('initiation_pts.csv',header=None)
termination_pts = pd.read_csv('termination_pts.csv',header=None)

#intialize variale i & j as zero
i=0
j=0

x = np.array(x_coor)
y= np.array(y_coor)
waves = np.array(waves) 
initiation_pts = np.array(initiation_pts) 
termination_pts = np.array(termination_pts) 

colors = np.random.uniform(low=0, high=255, size=(115,3))
colors = np.around(colors)
colors = np.array(colors, dtype=int)
rgb2hex = lambda r,g,b: '#%02x%02x%02x' %(r,g,b)
colors = [ rgb2hex(*colors[i,:]) for i in range(colors.shape[0]) ]

fig, ax = plt.subplots()
ax.scatter(x, y, s=500, c= 'white', edgecolors='blue')
plt.tick_params(axis=u'both', which=u'both',length=0, labelbottom = False, left=False, labelleft=False)
# plt.tick_params(left=False, labelleft=False) 
plt.box(False)

'''
This is the original code... moving it around and adding an append statement
'''
'''
for i in range(i):
    plt.text(x[0,i] * (1 - 0.019), y[0,i] * (1 - 0.017) , i+1, fontsize=8)  
arrows = []
'''
arrows=[]
n=len(arrows)
for i in range(n-1): #Range has to be expandable, so chaged it to the length of the arrows array minus 1
    plt.text(arrows.append(x[0,i] * (1 - 0.019), y[0,i] * (1 - 0.017) , i+1, fontsize=8))

def update(i):
    for arrow in arrows:
        arrow.remove()
        del arrow
    arrows[:] = []

    ax.scatter(x, y, s=500, c= 'white', edgecolors='blue')
    active_waves = np.unique(waves[:,i][waves[:,i] !=0])
    k=len(active_waves)
    for j in range(k-1):
       if j < 295:
            initiate = initiation_pts[0, active_waves[j]]
            terminate = termination_pts[0, active_waves[j]]
            nodes = (initiate, terminate)
            col = colors[active_waves[j]]
            fig = ax.scatter(x[0,initiate], y[0,initiate], s=500, c= col, edgecolors='blue', label='Wave' + str(active_waves[j]))
            arrow = ax.arrow(x[0,initiate], y[0,initiate],x[0,terminate] - x[0,initiate], y[0,terminate] - y[0,initiate],color = col,head_width=0.55)
            arrows.append(arrow)
       else:
           break

'''
Original code next two lines     --   
ani = FuncAnimation(fig, update)
ani.save('wave_direction.mp4', writer='ffmpeg')
'''

##Updated Code to run animation
anim = animation.FuncAnimation(fig, update)

#Still issues with getting it saved but looks like a syntax error.... 
'''
myWriter = animation.FFMpegWriter()
anim.save('wave_direction.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
'''
plt.show()

0 个答案:

没有答案