滑动条概述

滑动条是图形化界面(GUI)中常见的控件,matplotlib中的滑动条属于部件(widgets),matplotlib中的部件都是中性(neutral )的,即与具体后端实现无关。

滑动条具体实现定义为matplotlib.widgets.Slider类,继承关系为:Widget->AxesWidget->Slider

Slider类的签名为class matplotlib.widgets.Slider(ax, label, valmin, valmax, valinit=0.5, valfmt=None, closedmin=True, closedmax=True, slidermin=None, slidermax=None, dragging=True, valstep=None, orientation='horizontal', **kwargs)

Slider类构造函数的主要参数为:

  • ax:放置滑动条的容器,类型为matplotlib.axes.Axes的实例。
  • labels:滑动条标签文本,类型为字符串。
  • valmin:滑动条的最小值,类型为浮点数。
  • valmax:滑动条的最大值,类型为浮点数。
  • valinit:滑动条的初始值,类型为浮点数,默认为0.5
  • valfmt:滑动条值的%格式字符串,类型为字符串,默认为None
  • valstep:滑动条值的步长,类型为浮点数。
  • orientation:滑动条的布局方向,只有{'horizontal', 'vertical'}两个取值,默认为水平 'horizontal'

Slider类的属性为:

  • val:滑动条的当前值,类型为浮点数。

Slider类最常用的方法为:

  • on_changed( func):参数为回调函数,用于绑定滑动条值改变事件。
  • reset():将滑动条的值重置为初始值。
  • set_val(val):将滑动条的值设置为valval类型为浮点值。

案例:

在这里插入图片描述

代码分析

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

fig,ax = plt.subplots()
ax.pie([50,50],autopct='%1.2f%%')
plt.subplots_adjust(bottom=0.3)


axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
sfreq = Slider(axfreq, 'Freq', 0.0, 100.0,valfmt='% .2f', valinit=0, valstep=0.01)

def update(val):
    freq = sfreq.val
    ax.clear()
    ax.pie([freq,100-freq],autopct='%1.2f%%')
    fig.canvas.draw_idle()


sfreq.on_changed(update)
sfreq.reset()
sfreq.set_val(50.0)

plt.show()

滑动条相关方法原理解析

以下为Slider类部分源码,根据源码可知:

  • set_val(val):将滑动条的值设置为valval类型为浮点值。
  • reset():利用set_val(val)方法将当前值设为初始值。
  • 滑动条值改变事件在内部绑定的是_update方法,_update方法最终调用set_val(val)方法,参数val为需要更新的值,set_val(val)方法最终调用func(val)funcon_changed方法绑定的回调函数。所以回调函数func必须调用1个参数即当前选中的滑动条需要更新的值,如果在定义回调函数时不定义参数就会报错。
self.connect_event('button_press_event', self._update)
self.connect_event('button_release_event', self._update)
    
def _update(self, event):
    """Update the slider position."""
    if self.ignore(event) or event.button != 1:
        return

    if event.name == 'button_press_event' and event.inaxes == self.ax:
        self.drag_active = True
        event.canvas.grab_mouse(self.ax)

    if not self.drag_active:
        return

    elif ((event.name == 'button_release_event') or
            (event.name == 'button_press_event' and
            event.inaxes != self.ax)):
        self.drag_active = False
        event.canvas.release_mouse(self.ax)
        return
    if self.orientation == 'vertical':
        val = self._value_in_bounds(event.ydata)
    else:
        val = self._value_in_bounds(event.xdata)
    if val not in [None, self.val]:
        self.set_val(val)

def set_val(self, val):
    """
    Set slider value to *val*

    Parameters
    ----------
    val : float
    """
    xy = self.poly.xy
    if self.orientation == 'vertical':
        xy[1] = 0, val
        xy[2] = 1, val
    else:
        xy[2] = val, 1
        xy[3] = val, 0
    self.poly.xy = xy
    self.valtext.set_text(self._format(val))
    if self.drawon:
        self.ax.figure.canvas.draw_idle()
    self.val = val
    if not self.eventson:
        return
    for cid, func in self.observers.items():
        func(val)


def on_changed(self, func):
    """
    When the slider value is changed call *func* with the new
    slider value

    Parameters
    ----------
    func : callable
        Function to call when slider is changed.
        The function must accept a single float as its arguments.

    Returns
    -------
    int
        Connection id (which can be used to disconnect *func*)
    """
    cid = self.cnt
    self.observers[cid] = func
    self.cnt += 1
    return cid


def reset(self):
    """Reset the slider to the initial value"""
    if self.val != self.valinit:
        self.set_val(self.valinit)
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐