Python 使用matplotlib添加垂直滑块

Python 使用matplotlib添加垂直滑块,python,matplotlib,widget,slider,Python,Matplotlib,Widget,Slider,我想用matplotlib创建一个垂直滑块小部件,而不是水平滑块 我在matplotlib网页中找到了一个很好的示例,但我不知道如何在Y轴上移动滑块并更改滑块标签。我可以更改轴的位置,但不能更改滑块的移动。示例如下: import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button, RadioButtons fig, ax = plt.subplots() plt.

我想用matplotlib创建一个垂直滑块小部件,而不是水平滑块

我在matplotlib网页中找到了一个很好的示例,但我不知道如何在Y轴上移动滑块并更改滑块标签。我可以更改轴的位置,但不能更改滑块的移动。示例如下:

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

fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 3
s = a0*np.sin(2*np.pi*f0*t)
l, = plt.plot(t,s, lw=2, color='red')
plt.axis([0, 1, -10, 10])

axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.03, 0.25, 0.03, 0.65], axisbg=axcolor)
axamp = plt.axes([0.08, 0.25, 0.03, 0.65], axisbg=axcolor)
sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0)
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)

def update(val):
    amp = samp.val
    freq = sfreq.val
    l.set_ydata(amp*np.sin(2*np.pi*freq*t))
    fig.canvas.draw_idle()
sfreq.on_changed(update)
samp.on_changed(update)

resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
def reset(event):
    sfreq.reset()
    samp.reset()
button.on_clicked(reset)

rax = plt.axes([0.5, 0.025, 0.15, 0.15], axisbg=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0)
def colorfunc(label):
    l.set_color(label)
    fig.canvas.draw_idle()
radio.on_clicked(colorfunc)

plt.show()

从matplotlib 3.1开始,有一个方向关键字。在matplotlib 3.1中,这是不可能开箱即用的,因为
matplotlib.widgets.Slider
实现使用
axvspan
axvline
来定义滑块(这是一个
面片.Polygon
),并根据水平假设对其进行更新

如果仍在使用较旧版本的matplotlib,则以水平滑块为例编写自己的垂直滑块并不太困难(您也可以从
AxesWidget
中创建子类),但必须自己完成

自matplotlib 2.0起有效:下面给出了垂直滑块类;它的工作原理和水平的一样,只是。。。好。。。垂直

from matplotlib.widgets import AxesWidget
import six

class VertSlider(AxesWidget):
    """
    A slider representing a floating point range.

    For the slider to remain responsive you must maintain a
    reference to it.

    The following attributes are defined
      *ax*        : the slider :class:`matplotlib.axes.Axes` instance

      *val*       : the current slider value

      *hline*     : a :class:`matplotlib.lines.Line2D` instance
                     representing the initial value of the slider

      *poly*      : A :class:`matplotlib.patches.Polygon` instance
                     which is the slider knob

      *valfmt*    : the format string for formatting the slider text

      *label*     : a :class:`matplotlib.text.Text` instance
                     for the slider label

      *closedmin* : whether the slider is closed on the minimum

      *closedmax* : whether the slider is closed on the maximum

      *slidermin* : another slider - if not *None*, this slider must be
                     greater than *slidermin*

      *slidermax* : another slider - if not *None*, this slider must be
                     less than *slidermax*

      *dragging*  : allow for mouse dragging on slider

    Call :meth:`on_changed` to connect to the slider event
    """
    def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
                 closedmin=True, closedmax=True, slidermin=None,
                 slidermax=None, dragging=True, **kwargs):
        """
        Create a slider from *valmin* to *valmax* in axes *ax*.

        Additional kwargs are passed on to ``self.poly`` which is the
        :class:`matplotlib.patches.Rectangle` which draws the slider
        knob.  See the :class:`matplotlib.patches.Rectangle` documentation
        valid property names (e.g., *facecolor*, *edgecolor*, *alpha*, ...).

        Parameters
        ----------
        ax : Axes
            The Axes to put the slider in

        label : str
            Slider label

        valmin : float
            The minimum value of the slider

        valmax : float
            The maximum value of the slider

        valinit : float
            The slider initial position

        label : str
            The slider label

        valfmt : str
            Used to format the slider value, fprint format string

        closedmin : bool
            Indicate whether the slider interval is closed on the bottom

        closedmax : bool
            Indicate whether the slider interval is closed on the top

        slidermin : Slider or None
            Do not allow the current slider to have a value less than
            `slidermin`

        slidermax : Slider or None
            Do not allow the current slider to have a value greater than
            `slidermax`


        dragging : bool
            if the slider can be dragged by the mouse

        """
        AxesWidget.__init__(self, ax)

        self.valmin = valmin
        self.valmax = valmax
        self.val = valinit
        self.valinit = valinit
        self.poly = ax.axhspan(valmin, valinit, 0, 1, **kwargs)

        self.hline = ax.axhline(valinit, 0, 1, color='r', lw=1)

        self.valfmt = valfmt
        ax.set_xticks([])
        ax.set_ylim((valmin, valmax))
        ax.set_yticks([])
        ax.set_navigate(False)

        self.connect_event('button_press_event', self._update)
        self.connect_event('button_release_event', self._update)
        if dragging:
            self.connect_event('motion_notify_event', self._update)
        self.label = ax.text(0.5, 1.03, label, transform=ax.transAxes,
                             verticalalignment='center',
                             horizontalalignment='center')

        self.valtext = ax.text(0.5, -0.03, valfmt % valinit,
                               transform=ax.transAxes,
                               verticalalignment='center',
                               horizontalalignment='center')

        self.cnt = 0
        self.observers = {}

        self.closedmin = closedmin
        self.closedmax = closedmax
        self.slidermin = slidermin
        self.slidermax = slidermax
        self.drag_active = False

    def _update(self, event):
        """update the slider position"""
        if self.ignore(event):
            return

        if 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

        val = event.ydata
        if val <= self.valmin:
            if not self.closedmin:
                return
            val = self.valmin
        elif val >= self.valmax:
            if not self.closedmax:
                return
            val = self.valmax

        if self.slidermin is not None and val <= self.slidermin.val:
            if not self.closedmin:
                return
            val = self.slidermin.val

        if self.slidermax is not None and val >= self.slidermax.val:
            if not self.closedmax:
                return
            val = self.slidermax.val

        self.set_val(val)

    def set_val(self, val):
        xy = self.poly.xy
        xy[1] = 0, val
        xy[2] = 1, val
        self.poly.xy = xy
        self.valtext.set_text(self.valfmt % val)
        if self.drawon:
            self.ax.figure.canvas.draw_idle()
        self.val = val
        if not self.eventson:
            return
        for cid, func in six.iteritems(self.observers):
            func(val)

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

        A connection id is returned which can be used to disconnect
        """
        cid = self.cnt
        self.observers[cid] = func
        self.cnt += 1
        return cid

    def disconnect(self, cid):
        """remove the observer with connection id *cid*"""
        try:
            del self.observers[cid]
        except KeyError:
            pass

    def reset(self):
        """reset the slider to the initial value if needed"""
        if (self.val != self.valinit):
            self.set_val(self.valinit)
从matplotlib.widgets导入AxesWidget
进口六
类VertSlider(AxesWidget):
"""
表示浮点范围的滑块。
要使滑块保持响应,必须保持
参考它。
定义了以下属性
*ax*:滑块:class:`matplotlib.axes.axes`实例
*val*:当前滑块值
*hline*:a:class:`matplotlib.lines.Line2D`实例
表示滑块的初始值
*poly*:A:class:`matplotlib.patches.Polygon`实例
哪个是滑动旋钮
*valfmt*:用于格式化滑块文本的格式字符串
*label*:a:class:`matplotlib.text.text`实例
用于滑块标签
*closedmin*:滑块是否在最小值上关闭
*closedmax*:滑块是否在最大值时关闭
*slidermin*:另一个滑块-如果不是*None*,则此滑块必须为
大于*slidermin*
*slidermax*:另一个滑块-如果不是*None*,则此滑块必须为
小于*slidermax*
*拖动*:允许鼠标在滑块上拖动
调用:meth:`on_changed`连接到滑块事件
"""
定义初始值(self,ax,label,valmin,valmax,valnit=0.5,valfmt='%1.2f',
closedmin=True,closedmax=True,slidermin=None,
slidermax=None,Draging=True,**kwargs):
"""
在轴*ax*中创建从*valmin*到*valmax*的滑块。
额外的KWARG被传递到“self.poly”,这是
:class:`matplotlib.patches.Rectangle`用于绘制滑块
参见:class:`matplotlib.patches.Rectangle`文档
有效的属性名称(例如,*facecolor*、*edgecolor*、*alpha*、…)。
参数
----------
斧头:斧头
用于放置滑块的轴
标签:str
滑块标签
瓦尔明:浮动
滑块的最小值
瓦尔马克斯:浮动
滑块的最大值
缬氨酸:漂浮
滑块的初始位置
标签:str
滑块标签
valfmt:str
用于格式化滑块值,fprint格式字符串
克洛斯敏:布尔
指示滑块间隔是否在底部闭合
closedmax:bool
指示滑块间隔是否在顶部闭合
slidermin:滑块或无
不允许当前滑块的值小于
`滑鼠蛋白`
slidermax:滑块或无
不允许当前滑块的值大于
`滑块`
拖拉:布尔
如果可以用鼠标拖动滑块
"""
AxesWidget.\uuuuu初始化\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.valmin=valmin
self.valmax=valmax
self.val=valinit
self.valinit=valinit
self.poly=ax.axhspan(valmin、valnit、0、1、**kwargs)
self.hline=ax.axhline(valinit,0,1,color='r',lw=1)
self.valfmt=valfmt
ax.set_xticks([])
ax.set_ylim((最小值,最大值))
ax.set_-yticks([])
ax.set\u导航(False)
self.connect事件(“按钮按下事件”,self.\u更新)
self.connect\u事件(“按钮释放事件”,self.\u更新)
如果拖动:
自我连接事件(“运动通知事件”,自我更新)
self.label=ax.text(0.5,1.03,label,transform=ax.transAxes,
垂直对齐='中心',
水平对齐(“中心”)
self.valtext=ax.text(0.5,-0.03,valfmt%valinit,
transform=ax.transAxes,
垂直对齐='中心',
水平对齐(“中心”)
self.cnt=0
self.observer={}
self.closedmin=closedmin
self.closedmax=closedmax
self.slidermin=slidermin
self.slidermax=slidermax
self.drag\u active=False
def_更新(自身、事件):
“”“更新滑块位置”“”
如果self.ignore(事件):
返回
如果event.button!=1:
返回
如果event.name='button\u press\u event'和event.inaxes==self.ax:
self.drag\u active=True
event.canvas.grab_鼠标(self.ax)
如果未激活self.drag\u:
返回
elif((event.name='button\u release\u event')或
(event.name=='button\u press\u event'和
event.inaxes!=self.ax)):
self.drag\u active=False
event.canvas.release_鼠标(self.ax)
返回
val=event.ydata