Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plt.show() does nothing when used for the second time

I am just starting to learn data science using python on Data Camp and I noticed something while using the functions in matplotlib.pyplot

import matplotlib.pyplot as plt

year = [1500, 1600, 1700, 1800, 1900, 2000]
pop = [458, 580, 682, 1000, 1650, 6,127]

plt.plot(year, pop)

plt.show() # Here a window opens up and shows the figure for the first time

but when I try to show it again it doesn't..

plt.show() # for the second time.. nothing happens

And I have to retype the line above the show() to be able to show a figure again

Is this the normal thing or a problem?

Note: I am using the REPL

like image 976
Trachyte Avatar asked May 21 '18 15:05

Trachyte


People also ask

What does show () do in matplotlib?

If you are using Matplotlib from within a script, the function plt. show() is your friend. plt. show() starts an event loop, looks for all currently active figure objects, and opens one or more interactive windows that display your figure or figures.

Is PLT show () necessary?

Matplotlib is used in a IPython shell or a notebook (ex: Kaggle), plt. show() is unnecessary.

What happens if I dont use %Matplotlib inline?

In the current versions of the IPython notebook and jupyter notebook, it is not necessary to use the %matplotlib inline function. As, whether you call matplotlib. pyplot. show() function or not, the graph output will be displayed in any case.

Why is PLT plot blank?

The reason your plot is blank is that matplotlib didn't auto-adjust the axis according to the range of your patches. Usually, it will do the auto-adjust jobs with some main plot functions, such as plt. plot(), plt.


1 Answers

Answer

Yes, this is normal expected behavior for matplotlib figures.


Explanation

When you run plt.plot(...) you create on the one hand the lines instance of the actual plot:

>>> print( plt.plot(year, pop) )
[<matplotlib.lines.Line2D object at 0x000000000D8FDB00>]

...and on the other hand a Figure instance, which is set as the 'current figure' and accessible through plt.gcf() (short for "get current figure"):

>>> print( plt.gcf() )
Figure(432x288)

The lines (as well as other plot elements you might add) are all placed in the current figure. When plt.show() is called, the current figure is displayed and then emptied (!), which is why a second call of plt.show() doesn't plot anything.


Standard Workaround

One way of solving this is to explicitly keep hold of the current Figure instance and then show it directly with fig.show(), like this:

plt.plot(year, pop)
fig = plt.gcf()  # Grabs the current figure

plt.show()  # Shows plot
plt.show()  # Does nothing

fig.show()  # Shows plot again
fig.show()  # Shows plot again...

A more commonly used alternative is to initialize the current figure explicitly in the beginning, prior to any plotting commands.

fig = plt.figure()   # Initializes current figure
plt.plot(year, pop)  # Adds to current figure

plt.show()  # Shows plot
fig.show()  # Shows plot again

This is often combined with the specification of some additional parameters for the figure, for example:

fig = plt.figure(figsize=(8,8))

For Jupyter Notebook Users

The fig.show() approach may not work in the context of Jupyter Notebooks and may instead produce the following warning and not show the plot:

C:\redacted\path\lib\site-packages\matplotlib\figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure

Fortunately, simply writing fig at the end of a code cell (instead of fig.show()) will push the figure to the cell's output and display it anyway. If you need to display it multiple times from within the same code cell, you can achieve the same effect using the display function:

fig = plt.figure()   # Initializes current figure
plt.plot(year, pop)  # Adds to current figure

plt.show()  # Shows plot
plt.show()  # Does nothing

from IPython.display import display
display(fig)  # Shows plot again
display(fig)  # Shows plot again...

Making Use of Functions

One reason for wanting to show a figure multiple times is to make a variety of different modifications each time. This can be done using the fig approach discussed above but for more extensive plot definitions it is often easier to simply wrap the base figure in a function and call it repeatedly.

Example:

def my_plot(year, pop):
    plt.plot(year, pop)
    plt.xlabel("year")
    plt.ylabel("population")

my_plot(year, pop)
plt.show()  # Shows plot

my_plot(year, pop)
plt.show()  # Shows plot again

my_plot(year, pop)
plt.title("demographics plot")
plt.show()  # Shows plot again, this time with title
like image 163
WhoIsJack Avatar answered Sep 20 '22 11:09

WhoIsJack