Python 索引器:列表索引超出范围,在matplotlib图形、多线程程序中

Python 索引器:列表索引超出范围,在matplotlib图形、多线程程序中,python,pandas,multithreading,matplotlib,parallel-processing,Python,Pandas,Multithreading,Matplotlib,Parallel Processing,问题: 当运行代码时,在从开始到结束的非规则间隔出现以下错误 错误如下: 怀疑- 这是否与数据帧未完全更新和已绘制有关,可能是因为我正在尝试多线程处理 任何帮助 这是我的密码: #---------------------------------------------------------------------------------------# #创建功能块 # -----------------------------------------------------------

问题: 当运行代码时,在从开始到结束的非规则间隔出现以下错误

错误如下:

怀疑- 这是否与数据帧未完全更新和已绘制有关,可能是因为我正在尝试多线程处理

任何帮助

这是我的密码:

#---------------------------------------------------------------------------------------#
#创建功能块
# ---------------------------------------------------------------------------------------------------- #
def f_1m(开盘价、高价、低价):
全球旧时代
虽然(正确):
#只要*时间*尚未过去,就轮询当前的勾价
如果(新时间-旧时间)=1000:
打印('df_1m周期时间:',(新时间毫秒-旧时间毫秒),'ms')
旧时间=旧时间+1000
new_row={#5项的某些列表}
全球df_1m
df_1m=df_1m.append(新行,忽略索引=True)
打破
# ---------------------------------------------------------------------------------------------------- #
#绘制蜡烛图功能块
# ---------------------------------------------------------------------------------------------------- #
def f_plot_candle_图表(数据框、行、列、图表标题):
axs[行,列].cla()
axs[行,列].设置标题(图表标题)
axs[行,列].勾选参数(轴=x',旋转=90)
axs[行,列].绘图(数据框['Date'].tail(200),数据框['Close'].tail(200),标签='Close Price',颜色='blue',alpha=1,线宽=1.5)
axs[行,列].散点(数据框['Date'].tail(200),数据框['Buy_信号_价格'].tail(200),label='Buy',color='green',marker='^',alpha=1)
#并打印其他8行和散点
# ---------------------------------------------------------------------------------------------------- #
#更新XXm数据帧功能块
# ---------------------------------------------------------------------------------------------------- #
def f_xxm(df_1m、分钟、数据帧、绘图行、绘图列、dfname):
全球DFU结果
更新_df=False
#检查前导df_1m的长度是否已更新。
如果len(df_1m)>=分钟和len(df_1m)>(len(数据帧)*分钟)和(len(df_1m)-(len(数据帧)*分钟))==分钟:
更新_df=True
新行={'Date':Date'Open':df_1m['Open'][len(df_1m)-minutes],
“高”:df_1m.loc[pd.indexlice[len(df_1m)-minutes:len(df_1m)],“高”].max(),
“低”:df_1m.loc[pd.indexlice[len(df_1m)-minutes:len(df_1m)],“低”].min(),
“关闭”:df_1m['Close'][len(df_1m)-1]}
dataframe=dataframe.append(新行,忽略索引=True)
#运行交易功能:
dataframe_指示器=f_指示器(dataframe)
已处理=f_Alligator(数据帧_指示器,0,1,0,0,2.4)
数据帧_已处理=已处理[0]
#绘制图形并创建结果df
f_plot_candle_图表(已处理的数据框、plotrow、plotcolumn、dfname)
df_结果[dfname]=已处理[1],已处理[2],已处理[3],已处理[4],已处理[5],已处理[6],已处理[7],已处理[8],已处理[9],已处理[10],已处理[11],已处理[12],已处理[13],已处理[14],已处理[15],已处理[16],已处理[17],已处理[18]
返回更新_df,数据帧
# ---------------------------------------------------------------------------------------------------- #
#剩余工作(处理所有数据帧和绘图)功能块
# ---------------------------------------------------------------------------------------------------- #
def f_剩余工作(df_1m):
全球df_2m
全球DFU结果
全球df_1m_旧索引
如果len(df_1m)>0且df_1m.index.stop>df_1m_old_index:
df_1m_old_index=df_1m.index.stop
#运行交易功能:
df_1m_指示器=f_指示器(df_1m)
已处理=f_鳄鱼(df_1m_指示器,0,1,0,0,2.4)
df_1m_已处理=已处理[0]
df_结果['1m']=已处理[1],已处理[2],已处理[3],已处理[4],已处理[5],已处理[6],已处理[7],已处理[8],已处理[9],已处理[10],已处理[11],已处理[12],已处理[13],已处理[14],已处理[15],已处理[16],已处理[17],已处理[18]
#绘图图
f图、烛光图(df图、1m图、0、0、1m图)
#更新2米烛光图
结果=f_xxm(df_1m,2,df_2m,0,1,'2m')
如果结果[0]:
df_2m=结果[1]
#对所有其他蜡烛也要这样做
# ---------------------------------------------------------------------------------------------------- #
#主功能块
# ---------------------------------------------------------------------------------------------------- #
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#变量声明
old_time_ms=int(四舍五入(time.time()*1000))
#设置打印默认值并绘制空图形:
图,axs=plt子批次(2,3)
图suptitle(“蜡烛图”)
图设置紧密布局(真)
plt.style.use('bmh')
plt.draw()
#无限主回路
尽管如此:
以concurrent.futures.ThreadPoolExecutor()作为执行器:
#执行函数
p_1m=执行者提交(f_1m,0,0,0,)
p_剩余工作=执行者提交(f_剩余工作,df_1m)
plt.暂停(0.0001)

寻求调试帮助的问题(“为什么此代码不起作用?”)应包括所需的行为、特定的问题或错误,以及在问题本身中将其复制为格式化文本所需的最短代码。请创建一个链接并将其包含在您的帖子中。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 804, in callit
func(*args)
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\backends\_backend_tk.py", line 253, in idle_draw
self.draw()
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\backends\backend_agg.py", line 407, in draw
self.figure.draw(self.renderer)
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\artist.py", line 41, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\figure.py", line 1857, in draw
self.tight_layout(**self._tight_parameters)
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\cbook\deprecation.py", line 411, in wrapper
return func(*inner_args, **inner_kwargs)
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\figure.py", line 2613, in tight_layout
kwargs = get_tight_layout_figure(
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\tight_layout.py", line 303, in get_tight_layout_figure
kwargs = auto_adjust_subplotpars(fig, renderer,
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\tight_layout.py", line 84, in auto_adjust_subplotpars
bb += [ax.get_tightbbox(renderer, for_layout_only=True)]
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\axes\_base.py", line 4202, in get_tightbbox
bbox = a.get_tightbbox(renderer)
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\artist.py", line 278, in get_tightbbox
bbox = self.get_window_extent(renderer)
File "C:\Users\OOAKT\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\spines.py", line 172, in get_window_extent
tickstocheck = [self.axis.majorTicks[0]]
IndexError: list index out of range
# ---------------------------------------------------------------------------------------------------- #
# Create 1m candle Function block
# ---------------------------------------------------------------------------------------------------- #


def f_1m(open_price, high_price, low_price ):

    global old_time_ms

    while (True):
        
    # Poll current tick price as long as *time* has not passed
        if (new_time_ms - old_time_ms) <= 1000:
         # gather data for variables etc etc

    # If time has past, create output data
    elif (new_time_ms - old_time_ms) >= 1000:
        print('df_1m Cycle time:', (new_time_ms - old_time_ms), 'ms')
        old_time_ms = old_time_ms + 1000
        new_row = {# some list of 5 items}
        global df_1m
        df_1m = df_1m.append(new_row, ignore_index=True)
        break


# ---------------------------------------------------------------------------------------------------- #
# Plot Candle chart Function block
# ---------------------------------------------------------------------------------------------------- #


def f_plot_candle_chart(dataframe, row, column, charttitle):

    axs[row, column].cla()
    axs[row, column].set_title(charttitle)
    axs[row, column].tick_params(axis='x', rotation=90)
    axs[row, column].plot(dataframe['Date'].tail(200), dataframe['Close'].tail(200), label='Close Price', color='blue', alpha=1, linewidth=1.5)
    axs[row, column].scatter(dataframe['Date'].tail(200), dataframe['Buy_Signal_Price'].tail(200), label='Buy', color='green', marker='^', alpha=1)
    # and print 8 other lines and scatters

# ---------------------------------------------------------------------------------------------------- #
# Update XXm dataframes Function block
# ---------------------------------------------------------------------------------------------------- #


def f_xxm(df_1m, minutes, dataframe, plotrow, plotcolumn, dfname):

    global df_results
    update_df = False
    # check if the length of leading df_1m has updated.
    if len(df_1m) >= minutes and len(df_1m) > (len(dataframe)*minutes) and (len(df_1m) - (len(dataframe)*minutes)) == minutes:
        update_df = True
        new_row = {'Date': date, 'Open': df_1m['Open'][len(df_1m) - minutes],
               'High': df_1m.loc[pd.IndexSlice[len(df_1m) - minutes:len(df_1m)], 'High'].max(),
               'Low': df_1m.loc[pd.IndexSlice[len(df_1m) - minutes:len(df_1m)], 'Low'].min(),
               'Close': df_1m['Close'][len(df_1m) - 1]}
        dataframe = dataframe.append(new_row, ignore_index=True)
        # Run trading function:
        dataframe_Indicators = f_Indicators(dataframe)
        Processed = f_Alligator(dataframe_Indicators, 0, 1, 0, 0, 0, 2.4)
        dataframe_Processed = Processed[0]
        # Plot graphs adn create results df
        f_plot_candle_chart(dataframe_Processed, plotrow, plotcolumn, dfname)
        df_results[dfname] = Processed[1], Processed[2], Processed[3], Processed[4], Processed[5], Processed[6], Processed[7], Processed[8], Processed[9], Processed[10], Processed[11], Processed[12], Processed[13], Processed[14], Processed[15], Processed[16], Processed[17], Processed[18]

    return update_df, dataframe

# ---------------------------------------------------------------------------------------------------- #
# Residual work (process all dataframes and plot) Function block
# ---------------------------------------------------------------------------------------------------- #

def f_residual_work(df_1m):

    global df_2m
    global df_results
    global df_1m_old_index

    if len(df_1m) > 0 and df_1m.index.stop > df_1m_old_index:
        df_1m_old_index = df_1m.index.stop
        # Run trading function:
        df_1m_Indicators = f_Indicators(df_1m)
        Processed = f_Alligator(df_1m_Indicators, 0, 1, 0, 0, 0, 2.4)
        df_1m_Processed = Processed[0]
        df_results['1m'] = Processed[1], Processed[2], Processed[3], Processed[4], Processed[5], Processed[6], Processed[7], Processed[8], Processed[9], Processed[10], Processed[11], Processed[12], Processed[13], Processed[14], Processed[15], Processed[16], Processed[17], Processed[18]

        # Plot graphs
        f_plot_candle_chart(df_1m_Processed, 0, 0, '1m')

        # Update 2m candle chart
        results = f_xxm(df_1m, 2, df_2m, 0, 1, '2m')
        if results[0]:
            df_2m = results[1]
        # do this also for all the other candles

# ---------------------------------------------------------------------------------------------------- #
# Main Function block
# ---------------------------------------------------------------------------------------------------- #


if __name__ == '__main__':

    # variable declaration
    old_time_ms = int(round(time.time() * 1000))

    # Set plotting defaults and draw the empty figure:
    fig, axs = plt.subplots(2, 3)
    fig.suptitle('Candle Charts')
    fig.set_tight_layout(True)
    plt.style.use('bmh')
    plt.draw()

    # infinite main loop
    while True:
        with concurrent.futures.ThreadPoolExecutor() as executor:

            # execute the functions
            p_1m = executor.submit(f_1m, 0, 0, 0,)
            p_Residual_work = executor.submit(f_residual_work, df_1m, )
            plt.pause(0.0001)