我正在尝试以交互方式移动垂直线以可视化平分一些绘制的数据,但我似乎无法显示调整后的线......
import Tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class App:
def __init__(self, master, increment=.2, height=10):
self.increment = increment
# Create a container
frame = Tkinter.Frame(master)
# Make buttons...
self.button_left = Tkinter.Button(frame,text="< Move Left",
command=self.move_left)
self.button_left.pack(side="left")
self.button_right = Tkinter.Button(frame,text="Move Right >",
command=self.move_right)
self.button_right.pack(side="left")
fig = Figure()
ax = fig.add_subplot(111)
x = [3]*height
y = range(height)
#so that it's a tuple
self.line, = ax.plot(x, y)
self.canvas = FigureCanvasTkAgg(fig, master=master)
self.canvas.show()
self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
frame.pack()
def move_left(self):
x, y = self.line.get_data()
self.line.set_xdata(x-self.increment)
x, y = self.line.get_data()
print "x: {0}".format(x)
print "y: {0}".format(y)
#self.canvas.draw()
self.canvas.blit()
def move_right(self):
x, y = self.line.get_data()
self.line.set_xdata(x+self.increment)
x, y = self.line.get_data()
print "x: {0}".format(x)
print "y: {0}".format(y)
#self.canvas.draw()
self.canvas.blit()
root = Tkinter.Tk()
app = App(root)
root.mainloop()
我在这里查看了Joe Kingston的答案,但是当我最初将 x 和 y 坐标都提供给 plot() 时,事情似乎出了问题......