Home > database >  How to correctly handle the "AttributeError: module 'matplotlib.pyplot' has no attrib
How to correctly handle the "AttributeError: module 'matplotlib.pyplot' has no attrib

Time:06-25

I'm currently backtesting a trading strategy with around 286 trading pairs, and at the end of the loop I'm saving the plots with the backtest results in a particular path before repeating the process with the next trading pair.

The thing is, I realized that my memory available starts increasing as the number of plots saved increases, and that's something that I think can be avoided by releasing the memory properly.

However I tried two different options to do so, but all of them have failed, here's the first option I tried:

# Backtesting    
backtest = plt.figure(figsize=(16, 12), dpi=80)
backtest.title(f'plt of RSI for the {trading_pair} trading pair', fontsize = 20)
backtest.rc('xtick', labelsize = 10)
backtest.ylabel('Initial Investment (USDT)', font size=10)
backtest.plot(df_trading_days[rsi_period 4:] ,np.append(100, 100*np.cumprod(1   df_trading_pair["Return"])), label = "Buy & Hold Strategy")
backtest.plot(df_trading_days[rsi_period:] ,np.append(100, 100*np.cumprod(1   RSI_gross_return)), label = "RSI Strategy")
backtest.plot(df_trading_days[rsi_period:] ,np.append(100, 100*np.cumprod(1   RSI_net_return)), label = "RSI Strategy after fees")
backtest.legend(loc='lower right', font size = "large") 
backtest.savefig(f'C:/Users/ResetStoreX/Downloads/Binance futures data/Binance API KEY   Binance Wrapper/RSI Backtest/Backtesting of RSI for the {trading_pair} trading pair.png')
backtest.clear() #release memory
plt.close(backtest) #kill the figure

And I got the following output:

AttributeError: 'Figure' object has no attribute 'title'

<Figure size 1280x960 with 0 Axes>

Here's the second option I tried:

# Backtesting    
plt.figure(figsize=(16, 12), dpi=80)
plt.title(f'plt of RSI for the {trading_pair} trading pair', fontsize = 20)
plt.rc('xtick', labelsize = 10)
plt.ylabel('Initial Investment (USDT)', font size=10)
plt.plot(df_trading_days[rsi_period 4:] ,np.append(100, 100*np.cumprod(1   df_trading_pair["Return"])), label = "Buy & Hold Strategy")
plt.plot(df_trading_days[rsi_period:] ,np.append(100, 100*np.cumprod(1   RSI_gross_return)), label = "RSI Strategy")
plt.plot(df_trading_days[rsi_period:] ,np.append(100, 100*np.cumprod(1   RSI_net_return)), label = "RSI Strategy after fees")
plt.legend(loc='lower right', font size = "large") 
plt.savefig(f'C:/Users/ResetStoreX/Downloads/Binance futures data/Binance API KEY   Binance Wrapper/RSI Backtest/Backtesting of RSI for the {trading_pair} trading pair.png')
plt.clear() #release memory
plt.close() #kill the figure

And here's the output I got from that:

AttributeError: module 'matplotlib.pyplot' has no attribute 'clear'

So, I'm lost here. May I get some help to learn how to release my memory properly?

CodePudding user response:

After following the @Jan Wilamouski suggestion, I ended up getting another warning which was the following:

C:\Users\ResetStoreX\Downloads\Binance futures data\Binance API KEY Binance Wrapper\Massive Backtesting of RSI in a 30m timeframe for any crypto paired against USDT on Binance.py:143: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam figure.max_open_warning).

So it seems that it wasn't working, and after looking for other suggestions on Google, I ended up changing plt.figure().clear() for plt.clf() and after that I managed to keep my memory ram fine (around 37%, which was 4% less than before)

Final solution:

# Backtesting    
plt.figure(figsize=(16, 12), dpi=80)
plt.title(f'Backtesting of RSI for the {trading_pair} trading pair', fontsize = 20)
plt.rc('xtick', labelsize = 10)
plt.ylabel('Initial Investment (USDT)', fontsize=10)
plt.plot(df_trading_days[rsi_period 4:] ,np.append(100, 100*np.cumprod(1   df_trading_pair["Return"])), label = "Buy & Hold Strategy")
plt.plot(df_trading_days[rsi_period:] ,np.append(100, 100*np.cumprod(1   RSI_gross_return)), label = "RSI Strategy")
plt.plot(df_trading_days[rsi_period:] ,np.append(100, 100*np.cumprod(1   RSI_net_return)), label = "RSI Strategy after fees")
plt.legend(loc='lower right', fontsize = "large") 
plt.savefig(f'C:/Users/ResetStoreX/Downloads/Binance futures data/Binance API KEY   Binance Wrapper/RSI Backtest/Backtesting of RSI for the {trading_pair} trading pair.png')
plt.clf() #release memory
plt.close() #kill the figure
  • Related