Search code examples
pythonmatplotlibplotspyderdisplay

Matplotlib displaying plots on plot() instead of show() in console


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.


Solution

  • 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.