Skip to content

Instantly share code, notes, and snippets.

@paddymul
Created March 9, 2017 02:15
Show Gist options
  • Save paddymul/38cd010f81a66793827254b7e6a0f0fe to your computer and use it in GitHub Desktop.
Save paddymul/38cd010f81a66793827254b7e6a0f0fe to your computer and use it in GitHub Desktop.
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
class PlotController(object):
def __init__(self):
self.figs = []
def add_fig(self, new_fig):
print id(new_fig), "adding"
self.figs.append(new_fig)
def show_only(self, only_fig):
print id(only_fig)
for fig in self.figs:
if fig is only_fig:
print id(fig), "skipping because only fig"
continue
else:
print id(fig), "closing"
plt.close(fig)
axcolor = 'lightgoldenrodyellow'
a0, f0 = 5,3
t = np.arange(0.0, 1.0, 0.001)
control_obj = PlotController()
fig1, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
s = a0*np.sin(2*np.pi*f0*t)
l, = plt.plot(t, s, lw=2, color='red')
plt.axis([0, 1, -10, 10])
resetax1 = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax1, 'Only this', color=axcolor, hovercolor='0.975')
def show_only1(ev):
print "show only1"
control_obj.show_only(fig1)
button.on_clicked(show_only1)
control_obj.add_fig(fig1)
plt.draw()
#plt.show()
a0, f0 = 8, 9
fig2, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
s2 = a0*np.sin(2*np.pi*f0*t)
l2, = plt.plot(t, s2, lw=2, color='red')
plt.axis([0, 1, -10, 10])
resetax2 = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax2, 'Only this', color=axcolor, hovercolor='0.975')
def show_only2(ev):
control_obj.show_only(fig2)
print "show only2"
#plt.close(fig1)
control_obj.show_only(fig2)
button.on_clicked(show_only2)
control_obj.add_fig(fig2)
plt.draw()
ab = plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment