0

I'm trying to interactively move a vertical line to visualize bisecting some plotted data, but I can't seem to get the adjusted line to display...

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

I've looked at Joe Kingston's answer here, but things seem to go wrong when I give both x and y coordinates to plot() initially...

Community
  • 1
  • 1
  • what do you mean by 'go wrong'? – tacaswell Sep 25 '13 at 14:48
  • Sorry, I that isn't very clear. I meant that when I only include a single list of values (rather than x and y explicitly), things display fine. I realize why this was happening now (see below). – myedibleenso Sep 26 '13 at 16:37

1 Answers1

0

Well now I'm rightfully embarrassed. The line was just moving out of the visible area. I just needed to change the increment value or set a larger visible window with set_xlim()

Oops!

  • Glad to hear you sorted it out. Please remember to accept your own answer when it will let you (I think it is a 2 day waiting period). – tacaswell Sep 26 '13 at 17:57