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
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 447 Mar-11-2021, 10:52 AM
Last Post: buran
  Pyplot graph size TammyP 1 371 Jan-26-2021, 02:15 PM
Last Post: TammyP
  Understanding The Arguments for matplotlib.pyplot.plot JoeDainton123 0 428 Aug-19-2020, 01:19 AM
Last Post: JoeDainton123
  Pyplot and circle Reldaing 0 432 Apr-01-2020, 10:44 AM
Last Post: Reldaing
  Show graphs in matplotlib from a sql query? ScaleMan 1 635 Feb-06-2020, 05:47 PM
Last Post: ScaleMan
  How to continue code after .show() in matplotlib? jasper100125 1 2,794 Sep-09-2019, 02:44 AM
Last Post: luoheng
  .pyplot has not attribute zlim SchroedingersLion 5 4,108 Aug-25-2018, 01:31 PM
Last Post: SchroedingersLion
  Plot a graph using matplotlib.pyplot Tibas 1 1,952 Feb-23-2018, 10:47 AM
Last Post: Tibas
  show grayscale image using matplotlib zyb1003 1 14,394 Nov-01-2017, 11:45 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

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