In the following code, I have defined a plt
, but the show()
function is called if a condition is met.
while True:
# Get a key from keyboard and if it is 'q' exit the loop, otherwise do the following
fig,axes = plt.subplots(2,1, figsize=(20, 15))
should_plot = plot_dataframe(df, axes)
if should_plot == True:
for ax in axes:
ax.legend()
plt.show()
As you can see, plt.show()
is not called always, however, the subplot()
is called in each iteration of the loop. In the following situation
no plot
no plot
plot
I see three plot windows, two of them are empty and one shows the plot. Is there any way to delete the subplot on the else
part?
P.S: The question is different form this topic. Here, I don't want to delete a subplot within a plot area. In fact the close()
function is the right answer.