At the Spyder console (the REPL), I issue import matplotlib.pyplot as plt
. It is the topmost namespace, i.e., corresponding to globals()
.
In a Spyder startup file, I define a function to raise figure windows to the top.
def TKraiseCFG( FigID = None ):
import matplotlib.pyplot as plt
# The rest is just context
#-------------------------
# Assigning to plt unnecessary if
# imported plt is same object as in
# caller's scope
plt = inspect.currentframe().f_back.f_globals['plt'] # plt=globals()['plt']
# https://stackoverflow.com/a/78732915
if FigID is not None: plt.figure( FigID )
cfm = plt.get_current_fig_manager()
cfm.window.attributes('-topmost', True)
cfm.window.attributes('-topmost', False)
return cfm
Does the plt
in TKraiseCFG()
refer to the same object as plt
at the REPL?
Further context (not the main question): I can't imagine a console/REPL (or even multiple consoles) using more than one matplotlib.pyplot
. But I'm just getting to know Python, so I could be wrong. For the case of a single common matplotlib.pyplot
, however, I'm seeking a way to have it accessible to all scopes so that I can write convenience/utility functions like TKraiseCFG()
(which is just cobbled together after reading various pages, weeks ago). Unfortunately, my current method requires that code invoking TKraiseCFG()
contain a variable specifically named plt
referencing matplotlib.pyplot
.
Does the plt in TKraiseCFG() refer to the same object as plt at the REPL?
Yes it is the same object. Although I couldn't find matplotlib documentation that states this clearly.
You can test that the plt objects point to the same instance with the is
operator like this:
import matplotlib.pyplot as plt
def test_plt_equivalence():
import matplotlib.pyplot as plt2
print(plt2 is plt) # This will print True if plt and plt2 are the same object
test_plt_equivalence()