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

1 Answers1

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.