0
def matplotlib(i):

    graph_data = open('sampleData.txt', 'r').read()
    lines = graph_data.split('\n')
    xs = []
    ys = []
    for line in lines:
        if len(line) >1:
            x, y = line.split(',')
            xs.append(x)
            ys.append(y)

    fig.clear()

    'Limits'
    ax = plt.gca()
    ax.set_xlim([80, -80])
    ax.set_ylim([42, -42])
    plt.axis('equal')

    'Labels'
    plt.xticks([-16, -32, -48, -64, -80, 0, 16, 32, 48, 64, 80])
    plt.yticks([-42, -28, -14, 0, 14, 28, 42])

    plt.show()

    plt.scatter(xs, ys)

'Toolbar Buttons'

insertButt = Button(toolbar, text="Matplotlib TST", fg='Dark Red', bg="Dim Grey", activebackground='Dim Grey',
                    activeforeground='Dark Red', command=matplotlib).pack(side=LEFT, padx=2, pady=2)

ani = animation.FuncAnimation(fig, matplotlib, interval=1000)

The problem occurs when I try to run the function using a button. It works fine without the use of a button. I get the error,

matplotlib() missing 1 required positional argument: 'i'

I tried to fix this by simply removing the, i, then I got the error,

matplotlib() takes 0 positional arguments but 1 was given

I also tried putting (i) after command=matplotlib(i) in my button, my graph turned blank, and then I got this error,

matplotlib() takes 0 positional arguments but 1 was given

I am just trying to find to find out how to make a live graph, what the, i, even means, and I'm not sure why it doesn't work only when I run it with a button in Tkinter.

Sam
  • 47
  • 1
  • 1
  • 3

2 Answers2

1

This

command=matplotlib

executes function matplotlib() without argument

but this

FuncAnimation( matplotlib ) 

executes the same function with one argument.

So you get two different errors when you have def matplotlib(i): or def matplotlib():

You don't use i inside matplotlib() so you can assign default value None

def matplotlib(i=None):

and it will work with and without argument.


FuncAnimation sends "current frame number" as i and you can use to get different value from list (to display it) or to generate different plot (ie sin(i)).

furas
  • 134,197
  • 12
  • 106
  • 148
0

Based on what you have right now matplotlib(i) - i is really not getting used in your function so I would remove it so change your definition from matplotlib(i) to matplotlib().

Edited it based on comments.

Dilmer
  • 169
  • 8
  • Your last statement, or at least misleading. When associating the function with a button you do _not_ include parenthesis. – Bryan Oakley Dec 06 '16 at 00:07