I have a class that owns some matplotlib figures, axes, artists, etc. It has functions for manipulating and displaying these that all work fine when working with instances of the class.
However, if a user wants to make some separate matplotlib plots in, say, another cell of a notebook that contains instances of my class, all the class's figures get displayed when the user calls plt.show() to display their own figures.
I'm using ipympl, %matplotlib widget
, and setting plt.ioff().
Is there a way for me to set my figures so that they are not displayed when plt.show() is called?
Example: Consider these two Jupyter Notebook cells (have to be split for first figure to display)
%matplotlib widget
import matplotlib.pyplot as plt
plt.ioff()
class interactiveplot(object):
def __init__(self, x, y):
plt.ioff()
self.fig,self.ax = plt.subplots(figsize=(2,2))
self.plot, = self.ax.plot(x,y)
def update(self,x,y):
self.plot.set_data(x,y)
self.fig.canvas.draw()
def show(self):
return self.fig.canvas
#create class instance
test = interactiveplot([1,2,3,4],[4,1,5,7])
#display figure
test.show()
#Change data in displayed figure
test.update([2,3],[6,5])
#display a plot of something totally unrelated
fig,ax = plt.subplots(figsize=(2,2))
ax.scatter([2.3,3.4],[34,23])
plt.show()
#Both test.fig and fig are displayed! I only want to see fig!