Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
matplotlib.pyplot.show
#1
I am starting to learn about plotting and have a question about what plt.show() does.

I get the definition "The show() function in pyplot module of matplotlib library is used to display all figures", but can never find what figures this refers to. The two examples I looked at:
# sample code 
import matplotlib.pyplot as plt  
    
plt.plot([1, 2, 3, 4], [16, 4, 1, 8])  
plt.show()  
and

# Implementation of matplotlib function 
import matplotlib.pyplot as plt 
import numpy as np 
  
fig = plt.figure() 
x = np.arange(20) / 50
y = (x + 0.1)*2
  
val1 = [True, False] * 10
val2 = [False, True] * 10
  
plt.errorbar(x, y,  
             xerr = 0.1,  
             xlolims = True,  
             label ='Line 1') 
  
y = (x + 0.3)*3
  
plt.errorbar(x + 0.6, y,  
             xerr = 0.1, 
             xuplims = val1, 
             xlolims = val2, 
             label ='Line 2') 
  
y = (x + 0.6)*4
plt.errorbar(x + 1.2, y, 
             xerr = 0.1,  
             xuplims = True, 
             label ='Line 3') 
  
plt.legend() 
  
fig.suptitle('matplotlib.pyplot.show() Example') 
plt.show()
Seem to be not very good examples, because when I comment out the plt.show() lines and execute the code again, it displays exactly the same result as before, when plt.show() was not commented out.

Can someone explain to me (as if I were a fourth grader) what the purpose of plt.show() is?

Thanks in advance.
Reply
#2
Hi Alienspecimen

Interesting question, I found there is an interactive mode, in which plots are automatically displayed. You might be working in an environment which defaults to interactive mode. If you were to write a program and run it as a script, you typically would not be interactive mode. Here is the official docs and related functions:

https://matplotlib.org/stable/api/_as_ge...ive%20mode

Also see Appendix B in the below:

https://realpython.com/python-matplotlib-guide/
Reply
#3
(Feb-26-2021, 03:20 AM)Alienspecimen Wrote: it displays exactly the same result as before, when plt.show() was not commented out.
No it depend of what environment(Editor/IDE/Notebook) you run code in.
If run code from command line it will display nothing.
The purpose plt.show() is to generate a way to display the result when no external environment is used.

So if put code in a Notebook it disaply fine without plt.show().
But if i run same code from command line,it will display nothing without plt.show().
G:\div_code\div
λ python mp_test.py
# Nothing is shown
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PIL Image im.show() no show! Pedroski55 2 926 Sep-12-2022, 10:19 PM
Last Post: Pedroski55
  Is Matplotlib.pyplot Loaded DaveG 2 1,275 Apr-06-2022, 06:12 AM
Last Post: DaveG
  Python 3.10 not happy with Pandas and PyPlot? Danno 1 1,544 Feb-26-2022, 05:35 AM
Last Post: Danno
  PIL Image im.show() no show! Pedroski55 6 4,737 Feb-08-2022, 06:32 AM
Last Post: Pedroski55
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,161 Mar-11-2021, 10:52 AM
Last Post: buran
  Pyplot graph size TammyP 1 1,725 Jan-26-2021, 02:15 PM
Last Post: TammyP
  Understanding The Arguments for matplotlib.pyplot.plot JoeDainton123 0 1,900 Aug-19-2020, 01:19 AM
Last Post: JoeDainton123
  Pyplot and circle Reldaing 0 1,445 Apr-01-2020, 10:44 AM
Last Post: Reldaing
  Show graphs in matplotlib from a sql query? ScaleMan 1 1,817 Feb-06-2020, 05:47 PM
Last Post: ScaleMan
  How to continue code after .show() in matplotlib? jasper100125 1 9,241 Sep-09-2019, 02:44 AM
Last Post: luoheng

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020