1

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

2
  • Possible duplicate of matplotlib show() doesn't work twice Commented 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. Commented Jun 17, 2018 at 6:13

1 Answer 1

2

I did some research here matplotlib usage faq and here matplotlib show() doesn't work twice.

Here is what I learned:

  1. There is an interactive and non-interactive mode.
  2. 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.
  3. 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 the show() method to plot everything at once.
  4. While using non-interactive mode and using the show() method, all commands are stopped until the figure gets closed.
  5. There is no current solution to show a figure and then reshow it without replotting the same lines.
  6. You should check to see what the default mode is, interactive or non-interactive.
  7. To convert to the interactive mode, use matplotlib.pyplot.ion() method.
  8. To use non-interactive mode, use matplotlib.pyplot.ioff()

I have solved my problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.