1

I am learning how to use matplotlib now. From what I read, it seems the plot() function should be creating the plot and show() should display it. However, when I run the following, the display occurs on the plt.plot() line, not plt.show():

In [1]: import matplotlib.pyplot as plt

In [2]: %matplotlib inline

In [3]: plt.plot()
Out[3]: []

In [4]: plt.show()

The output above displays the empty plot after plot.plot(), but plt.show() does nothing. I am using Spyder with the iPython console. Running the code through the editor yields the same result. Am I misunderstanding what each function does individually or if I am not getting the correct results. Apologies if this is a stupid question.

For a bit of clarity:

My problem is not that the plot does not display. The plot displays perfectly! My issue is that so far, it seems the plt.show() function does not do anything. I can run my program, create a plot with plt.plot(x,y), change parameters such as the labels, and it displays as intended even if I completely omit plt.show(). plt.show() seems to have no functionality as it stands which is my issue.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
NaT3z
  • 344
  • 4
  • 13
  • Have you tried to plot some data? – Pierre Jun 09 '18 at 20:51
  • 1
    Possible duplicate of [How to show matplotlib plots in python](https://stackoverflow.com/questions/8575062/how-to-show-matplotlib-plots-in-python) – Pierre Jun 09 '18 at 20:52
  • No, `plt.plot()` _doesn't display any plot_ in your case, it just returns an empty list. – ForceBru Jun 09 '18 at 20:57
  • Hi @Pierre, I have tried plotting data. I can entirely run my code without calling the plt.show() function and when the program runs, the plot will display perfectly. For clarity, my issue is not that I am having problem displaying plots: it is that they are not displaying from the show() function. – NaT3z Jun 09 '18 at 21:06

3 Answers3

1

You are using the Jupyter/IPython Qt console (which is the only option in the newest version of Spyder). It may be configured in a way to show plots automatically from the cell they are created in. This option is known as inline backend, which you would get by setting the IPython magic to %matplotlib inline. In spyder such things may be configured via the

"Tools \ Preferences \ IPython console \ Graphics" options.

enter image description here

In case you choose any other option than inline, or deactivate "support for graphics" you should arrive at a situation where plt.show() is needed as usual to display the plot.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thank you so much! Now that I understand the cause, I'm happy to stick with the default backend and change it if I'm ever in a situation where I don't want to display plots inline. – NaT3z Jun 09 '18 at 22:11
  • If you ever want to show a plot a second time using the inline backend, you would then need to simply state the figure in a cell. Hence it makes sense to store handles to figures, `fig = plt.figure()`, such that in a later cell you may still state `fig` to show the plot. – ImportanceOfBeingErnest Jun 09 '18 at 22:14
0

If your environment defaults to interactive mode an you explicitly want to show your plot only when calling the show()-function than you can achieve this beaviour programatically by calling the ioff()-function.

plt.ioff()

I came across the same problem when using jupyter notebooks in VS Code and I wanted to control the "inline behavior". Try the following code. When commenting out the last line the plot is not displayed inline anymore.

import numpy as np
import matplotlib.pyplot as plt

plt.ioff() 

x = np.arange(0, 2*np.pi, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()

With this method I am able to switch off the inline-mode temporarily. So you can have a second code-block like this, to turn inline-mode back on.

plt.ion()
p = np.arange(0, 2*np.pi, 0.1)
q = np.sin(p)
plt.plot(p, q)

The first block only displays, when plt.show() is called. The second block displays inline, without calling plt.show()

-1

Use same code with: %matplotlib notebook in Jupiter Notebook IDE. Also, You need to use the code at one go as plt.show() works with previous commands.

hp_elite
  • 158
  • 1
  • 6