用于多个滑块的离散滑块,python

时间:2016-07-01 16:41:21

标签: python matplotlib

此处已解决了创建单个离散滑块的问题。

Can I make matplotlib sliders more discrete?

我希望能够创建多个离散滑块。

在python slider_demo页面中,(http://matplotlib.org/examples/widgets/slider_demo.html),可以通过slider_name.value访问多个滑块,其中value是更新功能所采用的属性。在离散滑块构造中,不能调用slider_name.value,因为离散滑块没有属性' value'。调用slider_name.set_val(value)会导致无限递归问题。

有谁知道如何从链接的构造中创建多个离散滑块?提前谢谢了。

1 个答案:

答案 0 :(得分:1)

根据我对this question的回答,可以使用多个PageSlider。它们不会更改原始Slider的基本原则,因此.valset_val仍然可用。

import matplotlib.widgets
import matplotlib.patches
import mpl_toolkits.axes_grid1

class PageSlider(matplotlib.widgets.Slider):

    def __init__(self, ax, label, numpages = 10, valinit=0, valfmt='%1d', 
                 closedmin=True, closedmax=True,  
                 dragging=True, **kwargs):

        self.facecolor=kwargs.get('facecolor',"w")
        self.activecolor = kwargs.pop('activecolor',"b")
        self.fontsize = kwargs.pop('fontsize', 10)
        self.numpages = numpages

        super(PageSlider, self).__init__(ax, label, 0, numpages, 
                            valinit=valinit, valfmt=valfmt, **kwargs)

        self.poly.set_visible(False)
        self.vline.set_visible(False)
        self.pageRects = []
        for i in range(numpages):
            facecolor = self.activecolor if i==valinit else self.facecolor
            r  = matplotlib.patches.Rectangle((float(i)/numpages, 0), 1./numpages, 1, 
                                transform=ax.transAxes, facecolor=facecolor)
            ax.add_artist(r)
            self.pageRects.append(r)
            ax.text(float(i)/numpages+0.5/numpages, 0.5, str(i+1),  
                    ha="center", va="center", transform=ax.transAxes,
                    fontsize=self.fontsize)
        self.valtext.set_visible(False)

        divider = mpl_toolkits.axes_grid1.make_axes_locatable(ax)
        bax = divider.append_axes("right", size="5%", pad=0.05)
        fax = divider.append_axes("right", size="5%", pad=0.05)
        self.button_back = matplotlib.widgets.Button(bax, label=ur'$\u25C0$', 
                        color=self.facecolor, hovercolor=self.activecolor)
        self.button_forward = matplotlib.widgets.Button(fax, label=ur'$\u25B6$', 
                        color=self.facecolor, hovercolor=self.activecolor)
        self.button_back.label.set_fontsize(self.fontsize)
        self.button_forward.label.set_fontsize(self.fontsize)
        self.button_back.on_clicked(self.backward)
        self.button_forward.on_clicked(self.forward)

    def _update(self, event):
        super(PageSlider, self)._update(event)
        i = int(self.val)
        if i >=self.valmax:
            return
        self._colorize(i)

    def _colorize(self, i):
        for j in range(self.numpages):
            self.pageRects[j].set_facecolor(self.facecolor)
        self.pageRects[i].set_facecolor(self.activecolor)

    def forward(self, event):
        current_i = int(self.val)
        i = current_i+1
        if (i < self.valmin) or (i >= self.valmax):
            return
        self.set_val(i)
        self._colorize(i)

    def backward(self, event):
        current_i = int(self.val)
        i = current_i-1
        if (i < self.valmin) or (i >= self.valmax):
            return
        self.set_val(i)
        self._colorize(i)


if __name__ == "__main__":
    import numpy as np
    from matplotlib import pyplot as plt


    num_pages = 15
    data = np.random.rand(9, 9, num_pages)

    fig, ax = plt.subplots()
    fig.subplots_adjust(bottom=0.22)

    im = ax.imshow(data[:, :, 0], cmap='viridis', interpolation='nearest', origin="lower")

    ax_slider = fig.add_axes([0.1, 0.07, 0.8, 0.04])
    ax_slider2 = fig.add_axes([0.1, 0.02, 0.8, 0.04])
    slider = PageSlider(ax_slider, 'Page', num_pages, activecolor="orange")
    slider2 = PageSlider(ax_slider2, 'Size', data.shape[0], valinit=8, activecolor="yellow")

    def update(val):
        i = int(slider.val)
        j = int(slider2.val)
        im.set_data(data[:j+1,:j+1,i])
        im.set_extent([-0.5,j+0.5,-0.5,j+0.5])
        fig.canvas.draw()


    slider.on_changed(update)
    slider2.on_changed(update)

    plt.show() 

enter image description here