Tkinter canvas and items that can use Matplotlib Toolbar? - python

I have written a small GUI program with Tkinter because I like the ability to queue updates to the mainloop for successive item updates in order to create an interative animation of changing circle colors that are induced by data changes.
( this is something I have found matplotib to not play nicely with, i.e. if I use patches and I want to update the color of the patch I have found that I have to add a new patch on top of the existing patch, which leaves color residue)
However, I would like to benefit from Matplotlib's Toolbar so that I can use the zoom and panning functionality.
I am experimenting on how to do this with a small test program for simplicity and have gotten matplotlib and tkinter in the same window... BUT the zoom doesnt work!
I have 2 weeks experience with Matplotlib and a couple days with Tkinter so please correct me on any inaccurate statements or add additional suggestions.
Here is the small program I have written for testing in which the matplotlib zoom doesn't work:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import sys
from Tkinter import *
import matplotlib.backends.backend_tkagg as tkagg
class App:
def __init__(self, master):
self.frame = Frame(master, bg='#FFFFFF', height=1000, width=1000,
borderwidth=1, relief=RAISED)
self.frame.pack(expand=YES, fill=BOTH)
self.button = Button(
self.frame, text="QUIT", fg="red", command=self.frame.quit
)
self.button.pack(side=BOTTOM)
self.hi_there = Button(self.frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=BOTTOM)
###
# Creating a canvas for Tk items
self.tkcanvas = Canvas(self.frame, height=600, width=600)
self.tkcanvas.config(background='yellow')
self.tkcanvas.pack()
self.tkcanvas.create_rectangle(0,0,100,100,fill='blue')
# Hoping to utilize the mpl toolbar for zooming function by doing:
self.fig = Figure(figsize=(4,.5))
self.mplcanvas = FigureCanvasTkAgg(self.fig, master=self.frame)
self.mplcanvas.get_tk_widget().pack(side='bottom', fill='both')
self.mplcanvas._tkcanvas.pack(side='bottom', fill='both', expand=1)
#
self.mpltoolbar = NavigationToolbar2TkAgg( self.mplcanvas, master )
self.mpltoolbar.update()
self.mpltoolbar.pack()
###
def say_hi(self):
print "hi there, everyone!"
root = Tk()
app = App(root)
root.mainloop()
root.destroy()

Related

How to overlay multiple plots on the same chart tkinter

I am new to tkinter and python and trying to write a simple program that lets me overlay line charts on the same plot. The code I have written (some taken from the 'matplotlib' site) keeps placing all the charts underneath each other. Is there a simpler way to do this. I cant seem to take the canvas or widgets outside the 'for' loop without it messing something up,my code and test 'csv' files are below.Any help or guidance please as to what I need to change?
from tkinter import ttk
import tkinter
from tkinter import *
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import numpy as np
root = tkinter.Tk()
root.wm_title("Embedding in Tk")
current_list = ['Blue', 'Green', 'Yellow']
for item in current_list:
x, y = np.loadtxt(item + '_' + 'Test.csv', skiprows=1, usecols=[2, 3],
unpack=True, delimiter=',')
fig = Figure(figsize=(2, 3), dpi=100)
fig.add_subplot(111).plot(x, y)
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
def on_key_press(event):
print("you pressed {}".format(event.key))
key_press_handler(event, canvas, toolbar)
canvas.mpl_connect("key_press_event", on_key_press)
def _quit():
root.quit() # stops mainloop
root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
button = tkinter.Button(master=root, text="Quit", command=_quit)
button.pack(side=tkinter.BOTTOM)
tkinter.mainloop()
# If you put root.destroy() here, it will cause an error if the window is
# closed with the window manager.
csv test files - Blue_Test.csv
Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8
1,100,19,100,Blue,2000,1.00E-19,1.00E-09
2,110,20,101,Blue,3000,5.00E-19,1.00E+00
3,120,21,102,Blue,4000,9.00E-19,2.00E+00
4,150,24,105,Blue,7000,2.10E-18,5.00E+00
5,160,25,106,Blue,8000,2.50E-18,6.00E+00
## csv test files - Green_Test.csv
Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8
1,100,19,2000,Green,2000,1.00E-19,1.00E-09
2,110,20,3001,Green,3000,5.00E-19,1.00E+00
3,120,21,4002,Green,4000,9.00E-19,2.00E+00
4,150,24,5005,Green,7000,2.10E-18,5.00E+00
5,160,25,6006,Green,8000,2.50E-18,6.00E+00
## csv test files - Yellow_Test.csv
Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8
1,100,19,11000,Yellow,2000,1.00E-19,1.00E-09
2,110,20,12001,Yellow,3000,5.00E-19,1.00E+00
3,120,21,13002,Yellow,4000,9.00E-19,2.00E+00
4,150,24,14005,Yellow,7000,2.10E-18,5.00E+00
5,160,25,15006,Yellow,8000,2.50000,5.000000
The problem lies in the for item in current_list: loop, in each iteration of the loop the code creates a new subplot and draws it on the canvas. Instead, you should create one subplot and plot each dataset on it. Then once the loop has completed, draw the populated subplot on the canvas. https://pydatascience.org/2017/11/24/plot-multiple-lines-in-one-chart-with-different-style-python-matplotlib/
import tkinter
import numpy as np
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
root = tkinter.Tk()
root.wm_title("Embedding in Tk")
current_list = ['Blue', 'Green', 'Yellow']
fig = Figure(figsize=(2, 3), dpi=100)
ax = fig.add_subplot(111)
for item in current_list:
x, y = np.loadtxt(item + '_' + 'Test.csv', skiprows=1, usecols=[2, 3],
unpack=True, delimiter=',')
ax.plot(x, y, label=item)
fig.legend()
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
def on_key_press(event):
print("you pressed {}".format(event.key))
key_press_handler(event, canvas, toolbar)
canvas.mpl_connect("key_press_event", on_key_press)
def _quit():
root.quit() # stops mainloop
root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
button = tkinter.Button(master=root, text="Quit", command=_quit)
button.pack(side=tkinter.BOTTOM)
tkinter.mainloop()
# If you put root.destroy() here, it will cause an error if the window is
# closed with the window manager.

How to resize matplotlib graph in tkinter window

I'm trying to set up some program that includes a matplotlib graph and tkinter buttons and whatnot below it. However, the matplotlib graph occupies the entire window overlaying the tkinter buttons and stuff.
I'd tried using pack, but it doesn't let me put stuff side by side, so I'd like the tkinter widgets to be arranged with .grid or coordinates.
from tkinter import *
from tkinter import StringVar
import tkinter
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
def eggs():
print("eggs")
root = Tk()
root.geometry("600x600")
root.title("eggs")
fig = Figure(figsize=(10, 6), dpi=100)
x = [1,2,3,4]
y = [1,2,3,4]
AS = [10/2**0]
fig.add_subplot(111).plot(x,y)
#fig.add_subplot(111).plot(AS)
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
testbutton = Button(root, text = "test button", command = eggs)
testbutton.place(x=100, y=550)
root.mainloop()
Wanting the top part of the window to be occupied by the graph and the buttons and other additional stuff below it.
You can use one Frame to keep graph and its toolbar vertically, and another Frame to keep buttons horizontally. And then you can use pack() to put one Frame at top top and other at the bottom.
The only problem makes figsize=(10, 6) which needs more space than "600x600"
BTW: you can use Button(toolbar, ...) to add button to NavigationToolbar2Tk - see "my tool".
]1
import tkinter as tk
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
# --- functions ---
def eggs():
print("eggs")
# --- main ---
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
AS = [10/2**0]
# ---
root = tk.Tk()
root.geometry("600x600")
root.title("eggs")
# ---
frame_top = tk.Frame(root)
frame_top.pack(fill='both', expand=True)
fig = Figure(dpi=100) # figsize=(10, 6),
fig.add_subplot(111).plot(x,y)
#fig.add_subplot(111).plot(AS)
canvas = FigureCanvasTkAgg(fig, master=frame_top) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(fill='both', expand=True)
toolbar = NavigationToolbar2Tk(canvas, frame_top)
toolbar.update()
tool = tk.Button(toolbar, text="my tool")
tool.pack(side='left')#, fill='x', expand=True)
# ---
frame_bottom = tk.Frame(root)
frame_bottom.pack(fill='x')
button1 = tk.Button(frame_bottom, text="button1")
button1.pack(side='left', fill='x', expand=True)
button2 = tk.Button(frame_bottom, text="button2")
button2.pack(side='left', fill='x', expand=True)
button3 = tk.Button(frame_bottom, text="button3")
button3.pack(side='left', fill='x', expand=True)
# ---
root.mainloop()

Overlay graph in tkinter

I am making an embeded matplotlib graph GUI program.
I want to make overlaid graphs in upper graph window when users click the "Update" button.
But, There is no response when I click "Update" button.
I am using Spyder Python 3.6 Version.
Below is what I wrote.
import matplotlib.pyplot as plt
import csv
import numpy as np
import tkinter as tk
from tkinter import ttk
import matplotlib as plt
plt.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from matplotlib import style
class Analysis_app(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
tk.Tk.wm_title(self, "SIM Analyser")
right_f = tk.Frame(self)
right_f.pack(side=tk.RIGHT)
self.entry1 = ttk.Entry(right_f).pack()
self.entry2 = ttk.Entry(right_f).pack()
self.entry3 = ttk.Entry(right_f).pack()
self.entry4 = ttk.Entry(right_f).pack()
self.entry5 = ttk.Entry(right_f).pack()
Button1 = ttk.Button(right_f, text='Update', command=self.plot).pack(side=tk.BOTTOM)
self.left_f = tk.Frame(self)
self.left_f.pack(side=tk.LEFT)
f = Figure(figsize=(10,6), dpi=100)
self.upplot = f.add_subplot(211)
self.botplot = f.add_subplot(212)
a =self.upplot
a.clear()
a.plot([1,2,3],[1,2,3])
canvas = FigureCanvasTkAgg(f, self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2TkAgg(canvas, self)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.mainloop()
def plot(self):
a =self.upplot
a.clear()
a.plot([1,2,3],[5,2,5])
You get no response because the canvas is not redrawn after changing the content of the plot.
The solution is to replace canvas = FigureCanvasTkAgg(f, self) by self.canvas = FigureCanvasTkAgg(f, self) so that you can redraw the canvas in self.plot:
def plot(self):
a = self.upplot
a.clear()
a.plot([1,2,3],[5,2,5])
self.canvas.draw_idle()
And then you should see the change after clicking on the update button.

Black box around matplotlib graph in Tkinter

I'm embedding a pyplot graph in a Tkinter frame. There's a thick black line around the entire figure that I'd like to get rid of. It only appears when my Tkinter window is actively selected:
I'm using Python 2.7.6 on Mac OS 10.9. Here's my code:
from Tkinter import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class PlotFrame(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.fig = plt.figure()
self.plot = self.fig.add_subplot(1, 1, 1)
self.canvas = FigureCanvasTkAgg(self.fig, master=self)
self.canvas.show()
self.canvas.get_tk_widget().grid(row=0, column=0, sticky=(N), padx=20, pady=20)
root = Tk()
plot_frame = PlotFrame(root)
plot_frame.grid(row=0, column=0)
root.mainloop()
Thanks!
That is controlled by the highlightthickness and highlightcolor attributes of the canvas widget. Set highlightthickness to zero if you don't want to see it.

animated NetworkX Graph in TkCanvas: background color

For a demonstration of a Graph algorithm i need to draw a networkx graph to a Tkinter Canvas and be able to modify that graph (and the plot) at runtime.
I have pieced together the following code (I hope it is the minimal code leading to my problem, but I'm new to this so I'm not sure):
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import Tkinter as Tk
import networkx as nx
from tkMessageBox import showinfo
root = Tk.Tk()
root.wm_title("Animated Graph embedded in TK")
root.wm_protocol('WM_DELETE_WINDOW', root.quit())
f = plt.figure(figsize=(5,4))
a = f.add_subplot(111)
plt.axis('off')
# the networkx part
G=nx.complete_graph(5)
nx.draw_networkx(G,pos=nx.spring_layout(G),ax=a)
# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
def next_graph():
if G.order():
a.cla()
G.remove_node(G.nodes()[-1])
nx.draw(G, pos=nx.circular_layout(G), ax=a)
canvas.draw()
b = Tk.Button(root, text="next",command=next_graph)
b.pack()
Tk.mainloop()
My problem now is this:
The first display of the graph is like I want it (backgroundcolor-wise), but after you first click 'Next' the backgroundcolor of the graph changes to white. I have tried changing the background color of the figure and the canvas.
I don't even know what brings that change about, I think it is simply drawing to the same canvas twice.
How can I modify the code to have the graph always have the same background color?
On an unrelated note: the root.quit() I added does not help in ending the application properly. This might be stupid on my side, but what did go wrong here?
I think you are very close. If you use nx.draw_networkx() in your event loop then it works (turn off the axis there too).
Here is your example with those modifications and also with a single layout computed at the beginning that is reused in the loop:
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import Tkinter as Tk
import networkx as nx
from tkMessageBox import showinfo
root = Tk.Tk()
root.wm_title("Animated Graph embedded in TK")
# Quit when the window is done
root.wm_protocol('WM_DELETE_WINDOW', root.quit)
f = plt.figure(figsize=(5,4))
a = f.add_subplot(111)
plt.axis('off')
# the networkx part
G=nx.complete_graph(5)
pos=nx.circular_layout(G)
nx.draw_networkx(G,pos=pos,ax=a)
xlim=a.get_xlim()
ylim=a.get_ylim()
# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
def next_graph():
if G.order():
a.cla()
G.remove_node(G.nodes()[-1])
nx.draw_networkx(G, pos, ax=a)
a.set_xlim(xlim)
a.set_ylim(ylim)
plt.axis('off')
canvas.draw()
b = Tk.Button(root, text="next",command=next_graph)
b.pack()
Tk.mainloop()

Categories