matplotlib show() not working. chart windows opens for 1second and closes automatically

Question:

I’m trying to plot a chart using matplotlib in python 3.10.5 but as soon as i run my code it generated the chart in a new windows and closes within seconds.

Here is my function:

def plot_all(levels, df):    
  fig, ax = plt.subplots(figsize=(16, 9))   
  candlestick_ohlc(ax,df.values,width=0.6, colorup='green', 
    colordown='red', alpha=0.8)    
  date_format = mpl_dates.DateFormatter('%d %b %Y')
  ax.xaxis.set_major_formatter(date_format)    
  for level in levels:        
    plt.hlines(level[1], xmin = df['Date'][level[0]], 
    xmax = 
      max(df['Date']), colors='blue', linestyle='--')    
  fig.show()

enter image description here

Asked By: Abhishek Pundir

||

Answers:

As per the original docs of matplotlib and issues raised on GitHub https://github.com/Eomys/pyleecan/issues/311
it has been found that you are supposed to use plt.show() for the output to stay.
Simply, replace your fig.show() with plt.show().

Answered By: The Myth