Matplotlib内存泄漏
我有一个循环,将财务OHLC价格数据转换为rgb像素数据。这个过程是: 1、获取OHLC价格数据 2、使用Matplotlib finance将OHLC数据绘制成图像 3、从图画布中获取rgb像素数据 对于这个程序,我将50个价格数据绘制到一个图表中,并将四个增量图表叠加到一个图表中,因此我得到(120*120*4)每个像素的数据。由于这将导致打开和关闭数千个matplotlib子库,因此我有严重的内存泄漏 我已经尝试了基本上所有的方法来处理这次泄漏,包括: 1,plt.close(“全部”) 2,del fig,ax 3,使用fig=figure.figure()代替fig,ax=plt.subplot() 我还尝试将matplotlib后端从Qt4agg更改为agg,但泄漏仍然存在。我甚至尝试过对这个过程进行多重处理,但仍然无法解决问题 代码如下:Matplotlib内存泄漏,matplotlib,memory-leaks,Matplotlib,Memory Leaks,我有一个循环,将财务OHLC价格数据转换为rgb像素数据。这个过程是: 1、获取OHLC价格数据 2、使用Matplotlib finance将OHLC数据绘制成图像 3、从图画布中获取rgb像素数据 对于这个程序,我将50个价格数据绘制到一个图表中,并将四个增量图表叠加到一个图表中,因此我得到(120*120*4)每个像素的数据。由于这将导致打开和关闭数千个matplotlib子库,因此我有严重的内存泄漏 我已经尝试了基本上所有的方法来处理这次泄漏,包括: 1,plt.close(“全部”)
def get_data(file_dir):
data = pd.DataFrame.from_csv(file_dir)
return data
file_dir = 'C:/Users/czzis/Desktop/DATA/EURUSD_ALL/DAT_MT_EURUSD_M1_2016.CSV'
data = get_data(file_dir)
b = []
start = 0
idx = 50
stack = 4
for i in range(22370):
a = np.zeros(shape = (120,120,1))
for i in range(stack):
opens = data.iloc[start+i : idx+i ,1].as_matrix()
highs = data.iloc[start+i : idx+i ,2].as_matrix()
lows = data.iloc[start+i : idx+i ,3].as_matrix()
closes = data.iloc[start+i : idx+i ,4].as_matrix()
# We would draw the chart first and then get the rgb data
# fig = figure.Figure()
# ax = fig.add_axes([1, 1, 1, 1])
# ax.set_axis_off()
fig, ax = plt.subplots()
plt.axis('off')
matplotlib.finance.candlestick2_ohlc(ax, opens, highs, lows, closes, width=0.6, colorup='w', colordown='k', alpha=1)
# print(i)
# del fig
# canvas = FigureCanvas(fig)
# fig.set_canvas(canvas)
fig.canvas.draw()
observation = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
observation = observation.reshape(fig.canvas.get_width_height()[::-1] + (3,))
# Process Data
observation = skimage.transform.resize(observation,(120,120))
observation = np.delete(observation,np.s_[1:4], axis = 2)
observation = np.reshape(observation,(120,120,1)) # a is the final shrinked image data we need
a = np.append(a,observation,axis = 2)
# matplotlib.pyplot.close('all')
# plt.clf()
plt.show()
plt.close('all')
# del fig,ax
# plt.close("all")
# del fig,ax
# print()
# del fig.canvas
observation = np.delete(a,np.s_[0:1],axis = 2)
start += 1
idx += 1
b.append(observation)
没有一种方法可以修复此漏洞。有人能帮我吗?谢谢 老问题,但最近在创建和保存数字时遇到了相同的问题。 尝试了所有你做过的事情,但没有成功 最后,在开始我的循环之前,添加下面的一行关闭Matplpotlib中的交互模式。 当运行+5000子块时,内存对我来说是合理的
plt.ioff()
这是一个老问题,但最近在创建和保存数字时遇到了相同的问题。 尝试了所有你做过的事情,但没有成功 最后,在开始我的循环之前,添加下面的一行关闭Matplpotlib中的交互模式。 当运行+5000子块时,内存对我来说是合理的
plt.ioff()
若要添加此项,我也在循环中尝试了gc.collect(),但它不会清除内存。我有相同的问题…:(若要添加此项,我也在循环中尝试了gc.collect(),但它也不会清除内存。我有相同的问题…:(