1

I am developing an application which takes input from user in .csv form and plots the graph for the corresponding values using matplotlib.

def plotgraph():
    x = []
    y = []
    data = text.get("1.0", END)
    sepFile = data.split('\n')

    for plotPair in sepFile:
        xAndY = plotPair.split(',')
        if len(xAndY[0]) != 0 and len(xAndY[1]) != 0:
            x.append(float(xAndY[0]))
            y.append(float(xAndY[1]))

    graph = Figure(figsize=(5,4), dpi=100)
    a = graph.add_subplot(111)
    a.plot(x,y)
    a.set_xlabel('Velocity')
    a.set_ylabel('Absorbance')
    canvas = FigureCanvasTkAgg(graph, master=RightFrame)
    canvas.show()
    canvas.get_tk_widget().grid(column=2, row=1, rowspan=2, sticky=(N, S, E, W))

I want a similar function like this Matplotlib: draw a selection area in the shape of a rectangle with the mouse in Tkinter which gives me the x0, x1, y0, y1 after the selection. I could make the already asked question make work & customize it according to my needs but unaware what I am doing mistake in __init__(self)

root = Tk()
class Annotate(object):
    def __init__(self):
        self.fig = mplfig.Figure(figsize=(1.5, 1.5))
        self.ax = self.fig.add_subplot(111)
        self.ax.plot([0,1,2,3,4],[0,8,9,5,3])        
        self.canvas = tkagg.FigureCanvasTkAgg(self.fig, master=root)
        self.x0 = None
        self.y0 = None
        self.x1 = None
        self.y1 = None
        self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
        self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
        self.ax.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)

When I run this code I get a blank Tk window. Can anyone tell me what should I do & what am I doing mistake

Community
  • 1
  • 1
Riq
  • 182
  • 1
  • 2
  • 17
  • Did you run this in console/terminal/cmd.exe ? Maybe there was some error message. If yes then add full error message in question. – furas Jul 14 '14 at 19:35
  • Did you run only this part of code with `root=Tk()` and `class Annotate(object)` ? If yes then your run only `root=Tk()` and you have to learn how to use classes. – furas Jul 14 '14 at 19:38
  • Your mistake: you didn't create object using class `Annotate` so python can't run code in `__init__`. – furas Jul 14 '14 at 19:40
  • @furas I am running my code in Spyder(which has a console). I am trying to work with your suggestions & see if things work out or not. Thanks for your suggestions. – Riq Jul 14 '14 at 20:05

2 Answers2

1

To use class you need at list something like this

class Annotate(object):
    def __init__(self):
        print "Annotate is runing"
        # rest of your code

root = Tk()
my_object = Annotate()

root.mainloop()

And probably you will need more work with this.

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thanks for your answer. I was not creating any object. Now the initial & final coordinates are showing up in the console but the selection area is not showing up..Can you please have a look at this http://stackoverflow.com/q/24748589/1581133 – Riq Jul 15 '14 at 01:31
0

I think now the simplest wat to do this is with the NavigationToolbar2Tk matplotlib class, which provides a built-in toolbar with a zoom selector, scroller, etc.

This is shown in the "Embedding in Tk" example, which in code code above would translate to something like:

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk

def plotgraph():
    x = []
    y = []
    data = text.get("1.0", END)
    sepFile = data.split('\n')

    for plotPair in sepFile:
        xAndY = plotPair.split(',')
        if len(xAndY[0]) != 0 and len(xAndY[1]) != 0:
            x.append(float(xAndY[0]))
            y.append(float(xAndY[1]))

    graph = Figure(figsize=(5,4), dpi=100)
    a = graph.add_subplot(111)
    a.plot(x,y)
    a.set_xlabel('Velocity')
    a.set_ylabel('Absorbance')
    canvas = FigureCanvasTkAgg(graph, master=RightFrame)
    canvas.show()
    canvas.get_tk_widget().grid(column=2, row=1, rowspan=2, sticky=(N, S, E, W))

    # set pack_toolbar to False to be able to use grid to set position
    toolbar = NavigationToolbar2Tk(canvas, RightFrame, pack_toolbar=False)
    toolbar.grid(column=2, row=3, stick="nw")
    toolbar.update() 
Matt Pitkin
  • 3,989
  • 1
  • 18
  • 32