基于 Tkinter 和 matplotlib 的交互式绘图 [英] Interactive plot based on Tkinter and matplotlib

查看:74
本文介绍了基于 Tkinter 和 matplotlib 的交互式绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的编程社区,

我正在尝试基于 Tkinter 和 pylab.plot 执行交互式绘图"以绘制一维值.abssissa 是一个一维的 numpy 数组 x 和 ordonates 值在一个多维数组 Y 中,例如.

I am trying to perform a "interactive plot" based on Tkinter and pylab.plot in order to plot 1D values. The abssissa are a 1D numpy array x and the ordonates values are in a multidimension array Y, eg.

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 矩阵的元素),并使用左右两个按钮在它们之间进行更改(以便处理更复杂的情况).通常我会创建一些像下面这样的函数来绘制图形.

I want to display y or y/2 (the elements of Y matrix) according to x and change between them with 2 buttons left and right (in order to go to more complex cases). Usually I create some functions like the following to plot graphs.

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

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

To add two buttons to change the value of nparameter, I have tried this without success :

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()

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

Can someone help me to understand how to perform such kind of feature ? Many thanks.

推荐答案

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

To change the y-values of the line, save the object that's returned when you plot it (line, = ax.plot(...)) and then use line.set_ydata(...). To redraw the plot, use canvas.draw().

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

As a more complete example based on your code:

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()

这篇关于基于 Tkinter 和 matplotlib 的交互式绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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