Python Forum

Full Version: How to automatically close mathplotlib output window?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was hoping I could display the output window for a few seconds then close it automatically, but that's not happening.

What am I doing wrong?

def countLetters(code):    
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    letter_counts = [code.count(l) for l in alphabet]
    letter_colors = plt.cm.hsv([0.8*i/max(letter_counts) for i in letter_counts])
    plt.bar(range(26), letter_counts, color=letter_colors)
    plt.xticks(range(26), alphabet) # letter labels on x-axis
    plt.tick_params(axis="x", bottom=False) # no ticks, only labels on x-axis
    plt.title("Frequency of each letter")
    plt.show() # added by me
    time.sleep(6)
    plt.close()
Running from command in non-interactive mode plt.show() GUI will block until the figures have been closed(bye you manually).
Can set plt.show(block=False) and also use plt.pause(6) it should work.
Tested work fine.
import matplotlib.pyplot as plt
import numpy as np

xaxis = np.array([2, 8])
yaxis = np.array([4, 9])
plt.plot(xaxis, yaxis)
plt.show(block=False)
plt.pause(6)
plt.close()
Also tested, works fine for me too!

Thank you very much!