【问题标题】:Interactive plot based on Tkinter and matplotlib基于 Tkinter 和 matplotlib 的交互式绘图
【发布时间】:2012-04-17 09:05:40
【问题描述】:

亲爱的编程社区,

我正在尝试基于 Tkinter 和 pylab.plot 执行“交互式绘图”以绘制一维值。横坐标是一维 numpy 数组 x,纵坐标值在多维数组 Y 中,例如。

import numpy
x = numpy.arange(0.0,3.0,0.01)
y = numpy.sin(2*numpy.pi*x)
Y = numpy.vstack((y,y/2))

我想根据 x 显示 y 或 y/2(Y 矩阵的元素),并使用左右 2 个按钮在它们之间进行切换(以便处理更复杂的情况)。通常我会创建一些类似下面的函数来绘制图表。

import pylab
def graphic_plot(n):
    fig = pylab.figure(figsize=(8,5))
    pylab.plot(x,Y[n,:],'x',markersize=2)
    pylab.show()

要添加两个按钮来改变nparameter的值,我试过这个没有成功:

import Tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class App:
def __init__(self,master):
    # Create a container
    frame = Tkinter.Frame(master)
    frame.pack()
    # Create 2 buttons
    self.button_left = Tkinter.Button(frame,text="<",command=self.decrease)
    self.button_left.pack(side="left")
    self.button_right = Tkinter.Button(frame,text=">",command=self.increase)
    self.button_right.pack(side="left")
    self.canvas = FigureCanvasTkAgg(fig,master=self)
    self.canvas.show()
def decrease(self):
    print "Decrease"
def increase(self):
    print "Increase"
root = Tkinter.Tk()
app = App(root)
root.mainloop()

有人可以帮助我了解如何执行此类功能吗?非常感谢。

【问题讨论】:

  • 您遇到什么错误?您能否详细说明“我已经尝试过但没有成功”?
  • 首先,我不能将图形和按钮包含在一个窗口中。其次,我不知道如何使用按钮更改n 参数的值。第三,点击按钮后我不知道如何“刷新”图表。
  • 我不知道其中的一些,但要让画布显示出来,你需要打包它。谷歌搜索了一下,看起来你可能想要:canvas.get_tk_widget().pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1)——或者你想打包它。 (我通常使用 .grid(),所以我不确定传递给 .pack() 的合理选项是什么)

标签: python user-interface matplotlib tkinter


【解决方案1】:

要更改线条的 y 值,请保存绘制时返回的对象 (line, = ax.plot(...)),然后使用 line.set_ydata(...)。要重绘绘图,请使用canvas.draw()

作为基于您的代码的更完整示例:

import Tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

class App:
    def __init__(self, master):
        # Create a container
        frame = Tkinter.Frame(master)
        # Create 2 buttons
        self.button_left = Tkinter.Button(frame,text="< Decrease Slope",
                                        command=self.decrease)
        self.button_left.pack(side="left")
        self.button_right = Tkinter.Button(frame,text="Increase Slope >",
                                        command=self.increase)
        self.button_right.pack(side="left")

        fig = Figure()
        ax = fig.add_subplot(111)
        self.line, = ax.plot(range(10))

        self.canvas = FigureCanvasTkAgg(fig,master=master)
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
        frame.pack()

    def decrease(self):
        x, y = self.line.get_data()
        self.line.set_ydata(y - 0.2 * x)
        self.canvas.draw()

    def increase(self):
        x, y = self.line.get_data()
        self.line.set_ydata(y + 0.2 * x)
        self.canvas.draw()

root = Tkinter.Tk()
app = App(root)
root.mainloop()

【讨论】:

  • 这几乎就是我想要的。谢谢你。你能解释一下这个语法self.line, = (特别是逗号)吗?我迷失了这个matplotlib工具(比如缩放......),有没有办法把它们带回来?
  • 是否可以有分离的窗口(一个在 pylab 中具有经典情节,第二个只有两个按钮)以使这件事更易于定制?
猜你喜欢
  • 1970-01-01
  • 2021-01-31
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-30
相关资源
最近更新 更多