Matplotlib memory leak -
i have loop turns financial ohlc price data rgb pixel data. process is:
1, ohlc price data
2, draw ohlc data images matplotlib finance
3, rgb pixel data fig.canvas
for program, drawing 50 price data 1 chart, , stacking 4 incremental chart one, getting (120*120*4) per pixel data. since result in opening , closing thousands of matplotlib subplots, having significant memory leak.
i have tried methods deal leak, including:
1, plt.close('all'),
2, del fig, ax
3, using fig = figure.figure(), instead of fig,ax = plt.subplots()
i tried changing matplotlib backend qt4agg agg, leak still exists. tried multiprocessing process, still won't solve problem.
the code here:
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 in range(22370): = np.zeros(shape = (120,120,1)) 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() # draw chart first , 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)) # final shrinked image data need = 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)
none of method can fix leak. me out? thanks!
Comments
Post a Comment