I wrote a very simple code in which I ask the user an input. If the user types "y"
I want to plot a linechart. If the user types "n"
the loop breaks.
I'm using Spyder IDE and I want the linechart to be plotted on a separate window in order to zoom and navigate the plot with the toolbar.
I encountered this problem, whenever I plot the plot windows freezes and it crashes (like in the image):
This is the code I've written:
import random
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
def plot_random():
x1 = np.linspace(0,100,100)
y1 = np.random.rand(100)
title = 'Title of the plot'
sns.set_style("whitegrid")
plt.ion()
line_plot = sns.lineplot(x=x1, y=y1)
line_plot.set_title(title)
line_plot.set(xlabel='xlabel', ylabel='ylabel')
plt.show()
plt.pause(5)
while True:
plotTF = input('Do you want to plot? (Y/N) ').lower()
if plotTF == 'n':
break
elif plotTF == 'y':
plot_random()
else:
print('Not a valid entry')
Before adding plt.pause(5)
the graph wasn't even plotted, but it popped a white plot window which immediately crashed.
Now it plots and crashes after the 5 second.
I want the plot to stay "zoomable" and "navigable" all the time till the user gives an other input.
How can I solve the problem?