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
  • 289,005
  • 45
  • 571
  • 615
NaT3z
  • 334
  • 3
  • 11
  • 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

2 Answers2

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
  • 289,005
  • 45
  • 571
  • 615
  • 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
-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
  • 129
  • 5