通过单击不同的按钮创建不同的滑块(matplotlib小部件)

时间:2018-08-06 12:14:49

标签: matplotlib button widget slider visible

好的,我们开始:该代码首先定义3个不同的设置为不可见的滑块(f010,f015,f022)和3个不同的按钮(WR10,WR15,WR22)。这些滑块均位于相同的xy位置。然后,每个按钮都应调用特定的滑块,例如按钮WR10->滑块f010。问题是,如果单击一个按钮,则会调用所有3个滑条并相互覆盖。有人可以帮我吗?

from __future__ import print_function
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

#Plot
axis_color = 'lightgoldenrodyellow'
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(111)
fig.subplots_adjust(left=0.25, bottom=0.35)

#Define Sliders and set all invisible
f010_slider_ax = fig.add_axes([0.25, 0.25, 0.65, 0.03], axisbg=axis_color)
f010_slider = Slider(f010_slider_ax, 'f0_10 [GHz]', 750.0, 1100.0, valinit=750.0)
f010_slider_ax.set_visible(False)

f015_slider_ax = fig.add_axes([0.25, 0.25, 0.65, 0.03], axisbg=axis_color)
f015_slider = Slider(f015_slider_ax, 'f0_15 [GHz]', 500.0, 750.0, valinit=500.0)
f015_slider_ax.set_visible(False)

f022_slider_ax = fig.add_axes([0.25, 0.25, 0.65, 0.03], axisbg=axis_color)
f022_slider = Slider(f022_slider_ax, 'f0_22 [GHz]', 340.0, 500.0, valinit=340.0)
f022_slider_ax.set_visible(False)


#Define WR buttons
WR10_button_ax = fig.add_axes([0.025, 0.9, 0.05, 0.05])
WR10_button = Button(WR10_button_ax, 'WR1.0:', color=axis_color, hovercolor='0.975')

WR15_button_ax = fig.add_axes([0.025, 0.8, 0.05, 0.05])
WR15_button = Button(WR15_button_ax, 'WR1.5:', color=axis_color, hovercolor='0.975')

WR22_button_ax = fig.add_axes([0.025, 0.7, 0.05, 0.05])
WR22_button = Button(WR22_button_ax, 'WR2.2:', color=axis_color, hovercolor='0.975')


#Define Event on Buttons
def WR10_button_on_clicked(mouse_event): 
    f010_slider_ax.set_visible(True)    
WR10_button.on_clicked(WR10_button_on_clicked)

def WR15_button_on_clicked(mouse_event):
    f015_slider_ax.set_visible(True)
WR15_button.on_clicked(WR15_button_on_clicked)

def WR22_button_on_clicked(mouse_event):
    f022_slider_ax.set_visible(True)
WR22_button.on_clicked(WR22_button_on_clicked)


plt.show()

0 个答案:

没有答案