The function in the code snippet below should create a simple matplotlib
figure and return it.
import matplotlib.pyplot as plt
import numpy as np
def random_figure():
fig, axes = plt.subplots(2, 1, squeeze=True)
for ax in axes:
ax.plot(*np.random.rand(10, 2))
return fig
What I don't understand is that the figure it is drawn more often than expected:
If I store the return value with
my_fig = random_figure()
, it is still drawn.If I just run
random_figure()
, it is shown twice:
Is this a setting in matplotlib I'm unaware of?
I'm using jupyter notebook in vscode, if that's relevant.
PS, my expectation is that it behave like the function
def random_number():
return np.random.rand()
which outputs nothing when the return value is stored with my_num = random_number()
.