Here is my problem:
I am learning python. I am trying to show 1 graph with 4 subgraphs on it.
The first time I use plt.show(), I get my figure. The 2nd time, I get nothing.
Does anyone know why and how to fix this?
Here is my code:
>>> import seaborn as sns
>>> import matplotlib.pyplot as plt
>>> anscombe = sns.load_dataset("anscombe")
>>> dataset_1 = anscombe[anscombe['dataset'] == 'I']
>>> dataset_2 = anscombe[anscombe['dataset'] == 'II']
>>> dataset_3 = anscombe[anscombe['dataset'] == 'III']
>>> dataset_4 = anscombe[anscombe['dataset'] == 'IV']
>>> fig = plt.figure()
>>> axes1 = fig.add_subplot(2,2,1)
>>> axes2 = fig.add_subplot(2,2,2)
>>> axes3 = fig.add_subplot(2,2,3)
>>> axes4 = fig.add_subplot(2,2,4)
>>> axes1.plot(dataset_1['x'], dataset_1['y'], 'o')
[<matplotlib.lines.Line2D object at 0x000002088592D8D0>]
>>> axes2.plot(dataset_2['x'], dataset_2['y'], 'o')
[<matplotlib.lines.Line2D object at 0x000002088592DE80>]
>>> axes3.plot(dataset_3['x'], dataset_3['y'], 'o')
[<matplotlib.lines.Line2D object at 0x0000020885941A58>]
>>> axes4.plot(dataset_4['x'], dataset_4['y'], 'o')
[<matplotlib.lines.Line2D object at 0x0000020885941A20>]
>>> plt.show()
>>> plt.show()
Edit: I have searched other posts, but they did not help me. One that I searched for in particular was this one:Thread that did not help
asked Jun 17, 2018 at 5:50
Chicken Sandwich No Pickles
3,57215 gold badges71 silver badges134 bronze badges
-
Possible duplicate of matplotlib show() doesn't work twicel'L'l– l'L'l2018年06月17日 06:00:51 +00:00Commented Jun 17, 2018 at 6:00
-
I edited my post, the link that you posted did not help me as I still don't know how to solve this problem.Chicken Sandwich No Pickles– Chicken Sandwich No Pickles2018年06月17日 06:13:18 +00:00Commented Jun 17, 2018 at 6:13
1 Answer 1
I did some research here matplotlib usage faq and here matplotlib show() doesn't work twice.
Here is what I learned:
- There is an interactive and non-interactive mode.
- While using interactive mode, don't close it because it gets completely erased in memory. Leave the figure open and it will update as you plot more lines or do whatever to it.
- While using non-interactive mode, you can plot as many lines as you want, but as soon as you call the
show()method, everything gets erased in memory and you have to replot it and then call theshow()method to plot everything at once. - While using non-interactive mode and using the
show()method, all commands are stopped until the figure gets closed. - There is no current solution to show a figure and then reshow it without replotting the same lines.
- You should check to see what the default mode is, interactive or non-interactive.
- To convert to the interactive mode, use
matplotlib.pyplot.ion()method. - To use non-interactive mode, use
matplotlib.pyplot.ioff()
I have solved my problem.
answered Jun 17, 2018 at 20:04
Chicken Sandwich No Pickles
3,57215 gold badges71 silver badges134 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py