title: How to correctly handle the "AttributeError: module 'matplotlib.pyplot' has no attribute 'clear'" when trying to release memory? python related
body: <p>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.</p>
  <p>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.</p>
  <p>However I tried two different options to do so, but all of them have failed, here's the first option I tried:</p>
  <pre><code># 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[&quot;Return&quot;])), label = &quot;Buy &amp; Hold Strategy&quot;)
  backtest.plot(df_trading_days[rsi_period:] ,np.append(100, 100*np.cumprod(1 + RSI_gross_return)), label = &quot;RSI Strategy&quot;)
  backtest.plot(df_trading_days[rsi_period:] ,np.append(100, 100*np.cumprod(1 + RSI_net_return)), label = &quot;RSI Strategy after fees&quot;)
  backtest.legend(loc='lower right', font size = &quot;large&quot;) 
  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
  </code></pre>
  <p>And I got the following output:</p>
  <blockquote>
  <p><strong>AttributeError:</strong> 'Figure' object has no attribute 'title'</p>
  </blockquote>
  <blockquote>
  <p><strong>&lt;Figure size 1280x960 with 0 Axes&gt;</strong></p>
  </blockquote>
  <p>Here's the second option I tried:</p>
  <pre><code># 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[&quot;Return&quot;])), label = &quot;Buy &amp; Hold Strategy&quot;)
  plt.plot(df_trading_days[rsi_period:] ,np.append(100, 100*np.cumprod(1 + RSI_gross_return)), label = &quot;RSI Strategy&quot;)
  plt.plot(df_trading_days[rsi_period:] ,np.append(100, 100*np.cumprod(1 + RSI_net_return)), label = &quot;RSI Strategy after fees&quot;)
  plt.legend(loc='lower right', font size = &quot;large&quot;) 
  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
  </code></pre>
  <p>And here's the output I got from that:</p>
  <blockquote>
  <p><strong>AttributeError:</strong> module 'matplotlib.pyplot' has no attribute 'clear'</p>
  </blockquote>
  <p>So, I'm lost here. May I get some help to learn how to release my memory properly?</p>