matplotlib:再次单击按钮后删除滑块

时间:2018-08-03 14:03:29

标签: python matplotlib button widget slider

我希望通过单击按钮WR3(='WR2.2')消失滑块(f01)。这样就没有覆盖层,并且滑块是新的。我不知道如何更新或删除此完整的滑块。这是我的代码:

from numpy import pi, sin
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
from matplotlib.patches import Ellipse
from scipy.optimize import fsolve
import pylab
w0=1.7
lamb=0.4119
f0=lamb*1.0/(299.792458)
#lamb=f0*1.0/(299.792458)
d_in=80.0
R1=46.672
R2=686.468
foc=1/((1/R1)+(1/R2))
c=299792458000.0
zR=np.pi*w0**(2)/(c/(f0*1000000000.0))
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(111)
x_start=0.0
x_end=500.0
ax.xaxis.set_ticks(np.arange(x_start,x_end+20.0, 20.0))
axis_color = 'lightgoldenrodyellow'
fig.subplots_adjust(left=0.25, bottom=0.35)
x = np.arange(x_start,x_end, 0.01)

w0_0 = 1.7
f0_0 = 727.82825
w1_0=w0*np.sqrt(1.0/(((d_in)/foc-1.0)**(2)+(zR/foc)**(2)))
d_in_0=80.0
d_out_0=foc+(d_in-foc)/(((d_in)/foc-1.0)**(2)+(zR/foc)**(2))

# function 1 and 2
def signal(w0,f0):
    return w0*np.sqrt(1.0+(x/(np.pi*w0**(2)/(c/(f0*1000000000.0))))**(2))
[line] = ax.plot(x, signal(w0_0, f0_0), linewidth=2, color='red')
WR3_button_ax = fig.add_axes([0.025, 0.7, 0.05, 0.05])
WR3_button = Button(WR3_button_ax, 'WR2.2:', color=axis_color, hovercolor='0.975')

# Define an axes area and draw a slider in it
w0_slider_ax  = fig.add_axes([0.25, 0.2, 0.65, 0.03], axisbg=axis_color)
w0_slider = Slider(w0_slider_ax, 'w0 [mm]', 0.1, 9.2, valinit=w0_0)

def WR3_button_on_clicked(mouse_event):
    f01_slider_ax = fig.add_axes([0.25, 0.25, 0.65, 0.03], axisbg=axis_color)
    f01_slider = Slider(f01_slider_ax, 'f0 [GHz]', 340.0, 500.0, valinit=d_in)
    w0_slider.set_val(1.5)
    f01_slider.set_val(340.0)
    def sliders_on_changed(val):
        line.set_ydata(signal(w0_slider.val, f01_slider.val))
        fig.canvas.draw_idle()
    f01_slider.on_changed(sliders_on_changed)

WR3_button.on_clicked(WR3_button_on_clicked)

plt.show()

我创建了此功能,以及有关滑块f01的功能。 谢谢

0 个答案:

没有答案