2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > python交互式绘图比较_python – 基于Tkinter和matplotlib的交互式绘图

python交互式绘图比较_python – 基于Tkinter和matplotlib的交互式绘图

时间:2019-05-04 11:40:45

相关推荐

python交互式绘图比较_python  – 基于Tkinter和matplotlib的交互式绘图

亲爱的编程共享美,

我正在尝试基于Tkinter和pylab.plot执行“交互式绘图”以绘制1D值. abssissa是1D numpy数组x,ordonates值在多维数组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矩阵的元素),并在左右两个按钮之间进行切换(为了更复杂的情况).通常我会创建一些如下的函数来绘制图形.

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="

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

有人可以帮我理解如何执行这样的功能吗?非常感谢.

解决方法:

要更改线的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()

标签:python,user-interface,matplotlib,tkinter

来源: https://codeday.me/bug/0902/1789921.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。