the_martian the_martian - 4 months ago 8
Python Question

matplot program gives unresponsive window when run

You'll notice that there is a bunch of missing code, it's for all the games being called from the menu. I removed it because it is long and not needed for my question. My issue is only with a particular function,

display_data
. When I call it from the menu, matplot opens in a new window and just crashes immediately. I just set up the basic plot below for testing. Any ideas?

import matplotlib.pyplot as plt

def display_data():
plt.plot([1,2,4],[2,7,9])
plt.show()

# (6) Save Progress ------------------------------------------------------------

# (7) Load Data ----------------------------------------------------------------

# (8) Quit Game ----------------------------------------------------------------

def quit_game():
print('\nThank you for playing!')

# Main Menu --------------------------------------------------------------------

def menu():
calculation_game = print("\nEnter 1 to play 'Calculation'")
bin_reader = print("Enter 2 to play 'Binary Reader'")
trifacto = print("Enter 3 to play 'Trifacto'")
statistics = print("Enter 4 to view your statistics")
display_data = print("Enter 5 to display data")
save_game = print("Enter 6 to save your progress")
load_data = print("Enter 7 to load data")
quit_game = print("Enter 8 to quit the game")

def main_menu():
print('Welcome to BrainAge!')
main_record = []
user_input = ''
while user_input != '8':
menu()
user_input = input('\nWhat would you like to do? ')
if user_input == '1':
calculation_game()
if user_input == '2':
binary_reader_game()
if user_input == '3':
trifacto_game()
if user_input == '4':
display_statistics()
if user_input == '5':
display_data()
if user_input == '8':
quit_game()

main_menu()

Answer

I had a similar problem a while back, try setting this line

plt.show()

to

plt.show(block=True)

According to the docs this overrides the blocking behaviour caused by running matplotlib.pyplot in interactive mode, which is known to cause issues in some environments. I believe this only works for the latest version of matplotlib.

Comments