尝试绘制循环时出现此错误。它在try / except中发生,所以我没有完整的堆栈跟踪,只是“分配位图失败”,然后程序退出。从我可以看到这是一个matplotlib错误,人们建议运行clf()。我确实绘制了大量数据,但我认为这不足以使事情搞砸。无论如何,这里基本上就是代码。
def plot_data(data):
pyplot.clf()
buys_X,buys_y,sells_X,sells_y = [],[],[]
for index,row in data.iterrows():
if row['action'] == 'BUY':
buys_X.append(row['time'])
buys_y.append(row['price'])
elif row['action'] == 'SELL':
sells_X.append(row['time'])
sells_y.append(row['price'])
data_cols = ['prof','price','quantity','pred']
fig,axs = pyplot.subplots(len(data_cols),1,constrained_layout=True,figsize=(15,12))
for idx,col in enumerate(data_cols):
if col == 'price':
axs[idx].plot(data['time'],data[col],linewidth=0.75)
axs[idx].scatter(buys_X,color='red',s=12.5)
axs[idx].scatter(sells_X,sells_y,color='green',s=12.5)
elif col == 'pred':
axs[idx].plot(data['time'],data['curr_ratio'],label='curr_ratio')
axs[idx].plot(data['time'],data['pred'],label='pred')
axs[idx].axhline(0.5,color='black')
axs[idx].legend()
elif col == 'prof':
axs[idx].plot(data['time'],data[col])
axs[idx].axhline(0,color='black')
elif col == 'quantity':
axs[idx].plot(data['time'],data[col])
axs[idx].set_title(col)
axs[idx].xaxis.grid(True)
if idx == len(data_cols) - 1:
for tick in axs[idx].get_xticklabels():
tick.set_rotation(45)
else:
axs[idx].xaxis.set_ticklabels([])
pyplot.savefig('plots.png',dpi=100)
因此,我循环运行此代码,并向该函数提供数据帧。每次添加一行时,我都会进行一次新绘图,并且每分钟都会添加一个新行。但是我在程序运行约3-4小时后收到此错误。任何想法为什么会发生这种情况以及我可以做些什么来解决呢?我尝试降低dpi,这似乎有帮助,但是我不确定这是否与错误有关。