So I tried to write a custom plotting function, which will behave like plot
, in that you can call subplot
and then calls to new_plot
will be directed to that subplot, subsequent calls will plot over top of the original plot in different colors, etc.
However, when I try to plot multiple times from the interactive prompt in IPython, only the first call works. If I call multiple times in a script or in a single command, it works.
I've stripped out everything except the bare minimum that demonstrates the problem:
import matplotlib.pyplot as plt
from numpy.random import randn
def new_plot():
# get a figure/plot
ax = plt.gca()
# Plot the poles and set marker properties
ax.plot(randn(10), randn(10))
if __name__ == "__main__":
plt.subplot(2, 1, 1)
new_plot()
new_plot()
plt.subplot(2, 1, 2)
new_plot()
new_plot()
plt.show()
Running this script works fine, producing 2 plots of different colors on top of each other in each subplot. Also, calling the function twice in one line of IPython interactive prompt works fine:
In [3]: new_plot()
...: new_plot()
...:
However, calling the function twice in a row does not work, only the first call succeeds, the rest fail silently:
In [4]: new_plot()
In [5]: new_plot()
Whyyyyyyyyyyyyyyyyy?