Python Matplotlib 滑块小部件未更新 [英] Python Matplotlib slider widget is not updating

查看:40
本文介绍了Python Matplotlib 滑块小部件未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用包含数据和一个(matplotlib)滑块小部件的多个matplotlib画布.问题是滑块小部件没有正确更新(看起来没有发送鼠标事件之类的)

i want to use multiple matplotlib canvasses which contain data + a (matplotlib) slider widget. the problem is that the slider widgets are not updating correctly (looks like the mouse events are not sent or something)

这就是我所拥有的:

import matplotlib
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib.widgets import Slider

class PlotWidget( Tk.Frame ):

    def __init__(self, master):
        Tk.Frame.__init__(self, master)
        self.figure = Figure(figsize=(5,4), dpi=75)
        self.canvas = FigureCanvasTkAgg(self.figure, self)

        self.frame = self.canvas.get_tk_widget()
        self.frame.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
        self.canvas.show()

    def add_slider(self):
       a = self.figure.add_axes([0.25, 0.1, 0.65, 0.03], axisbg='lightgoldenrodyellow')
       s = Slider( a, 'range', 0.1, 30.0, valinit=5)
       self.canvas.show()



root = Tk.Tk()

option = 1    

if option == 1 or option = 2:
     w =  PlotWidget(root)
     w.pack()
     figure = w.figure
else:
     f = Tk.Frame(root, bd = 6, bg='red')
     figure = matplotlib.figure.Figure(figsize=(5,4), dpi=75)
     canvas = FigureCanvasTkAgg(figure, f)
     canvas.get_tk_widget().pack()
     canvas.show()
     f.pack()


if option = 1:
    w.add_slider()
else:
    a = figure.add_axes([0.25, 0.1, 0.65, 0.03], axisbg='lightgoldenrodyellow')   
    s = Slider( a, 'range', 0.1, 30.0, valinit=5)

Tk.mainloop()

选项1不适用于所有其他组合(在测试脚本中,至少在我的应用程序中,当我在另一个位置创建滑块小部件时,它也不起作用.滑块对鼠标事件没有反应.....

option 1 does not work all other combinations do (in the test script atleast, in my application it also does not work when i create the slider widget in another location. the slider does not react on the mouse events.....

我错过了什么?!

推荐答案

我必须进行以下更改才能使您的脚本在我的计算机上运行.

I had to make the following changes to get your script to run on my machine.

import matplotlib
from Tkinter import *
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib.widgets import Slider

class PlotWidget( Frame ):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.figure = Figure(figsize=(5,4), dpi=75)
        self.canvas = FigureCanvasTkAgg(self.figure, self)

        self.frame = self.canvas.get_tk_widget()
        self.frame.pack(side=TOP, fill=BOTH, expand=1)
        self.canvas.show()

    def add_slider(self):
       a = self.figure.add_axes([0.25, 0.1, 0.65, 0.03], axisbg='lightgoldenrodyellow')
       s = Slider( a, 'range', 0.1, 30.0, valinit=5)
       self.canvas.show()



root = Tk()

option = 1

if option == 1 or option == 2:
     w =  PlotWidget(root)
     w.pack()
     figure = w.figure
else:
     f = Frame(root, bd = 6, bg='red')
     figure = matplotlib.figure.Figure(figsize=(5,4), dpi=75)
     canvas = FigureCanvasTkAgg(figure, f)
     canvas.get_tk_widget().pack()
     canvas.show()
     f.pack()


if option == 1:
    w.add_slider()
else:
    a = figure.add_axes([0.25, 0.1, 0.65, 0.03], axisbg='lightgoldenrodyellow')   
    s = Slider( a, 'range', 0.1, 30.0, valinit=5)

root.mainloop()

由于创建 Slider 的范围,我认为选项 1 不起作用.

I believe option 1 doesn't work due to the scope of which the Slider is being created in.

示例此处也在与图中相同的范围内创建并显示命令.

The example here also creates it in same scope as the figure and show commands.

如果您想将Slider的创建移动到一个函数中,则可以将该Slider返回到该函数之外的作用域,添加为add_slider的最后一行 return ,然后更改 w.add_slider() s = w.add_slider()

If you wanted to move the Slider creation into a function you have that function return the Slider back to the scope outside the function, add as the last line of add_slider return s and then change w.add_slider() to s = w.add_slider()

或者您可以将相关内容存储在与画布和图形相同的范围(类成员)中,例如 self.slider = Slider( a, 'range', 0.1, 30.0, valinit=5).

or you could store the relevant in the same scope (class member) as canvas and figure e.g self.slider = Slider( a, 'range', 0.1, 30.0, valinit=5).

option = 1 时,这两种方法都会使滑块更新鼠标事件.

Both of these methods make the slider update on mouse event when option = 1.

这篇关于Python Matplotlib 滑块小部件未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆