K4n3
K4n3

Reputation: 53

Python: Is there a way to suppress a function's plotting but keep the return?

I have a function that generates an image, plots it in a pyplot figure and then returns the image as well. The function is long and it isn't entirely reasonable to find where exactly it actually does the plotting. Does anyone know of a way to simply suppress the plots but let the function run normally so I can get the returned images? This is in python 3.6 if that helps.

Upvotes: 1

Views: 1438

Answers (1)

user2722968
user2722968

Reputation: 16495

You could monkey-patch the function's namespace and replace the plotting function with a no-op function. Given

'''This is the to-be patched code, living in some module'''

def plot():
    print('Here is a plot: ##====>')

def do_the_plot():
    plot()

And then

'''Our new code, living some place else'''
def newplot():
    print('I wont do it!')

# The globals() will be replaced by the actual module names
globals()['plot'] = globals()['newplot']
do_the_plot()

prints I wont do it!

Upvotes: 1

Related Questions