[Tutor] Matplotlib in Tkinter

Albert-Jan Roskam sjeik_appie at hotmail.com
Sun Feb 26 01:22:02 EST 2017


(Sorry for top-posting)

Hi, I recently ran into the same problem when I wanted to embed an interactive Matplotlib choropleth in my Tkinter app. I solved it by creating a separate class MyPlot for plot + toolbar (it inherited from Tkinter.Frame). MyPlot internally uses the pack geometry manager as given in the example from the mpl website. Then, you use MyPlot as 'one unit', and you use the grid method on its instances.

Best wishes,
Albert-Jan
________________________________
From: Tutor <tutor-bounces+sjeik_appie=hotmail.com at python.org> on behalf of Pooja Bhalode <poojabhalode11 at gmail.com>
Sent: Sunday, February 19, 2017 7:36:32 PM
To: tutor at python.org
Subject: [Tutor] Matplotlib in Tkinter

Hi,

I am trying to create a graph in Tkinter window. And following is a snipet
of the code.

Code:
  simroot = Toplevel(root)
simroot.title("Simulation of experiments")
Label(simroot, text = "Displaying concentration profiles with respect to
time:").pack(side = TOP)

Label(simroot, text = "Select the experiments to be displayed: ").pack(side
= TOP)
optionexp = ['Experiment 1', 'Experiment 2', 'Experiment 3', 'Experiment
4', 'Experiment 5']
expsim = StringVar()
dropexp = OptionMenu(simroot, expsim, *optionexp)
dropexp.pack(side = TOP)
dropexp.config(width = 15)

# Row 1 would print the name of the experiment selected and the species
displayed would be in row3.
Label(simroot, text = "Select concentration species:").pack(side = TOP)
concsim = StringVar()
optionlistsim = ['A', 'B', 'C', 'D', 'E']
dropsim = OptionMenu(simroot, concsim, *optionlistsim)
dropsim.pack(side = TOP)
dropsim.config(width = 8)
Label(simroot, text = "").pack(side = LEFT)

def Submitplot():
print "Create plot"

f = Figure(figsize = (5,5), dpi = 80)
a = f.add_subplot(111)
a.plot([1,2,3,4,5],[10,11,12,14,15])

canvas = FigureCanvasTkAgg(f, simroot)
canvas.show()
canvas.get_tk_widget().pack(side = BOTTOM)

toolbar = NavigationToolbar2TkAgg(canvas, simroot)
toolbar.update()
canvas._tkcanvas.pack(side = BOTTOM)
Button(simroot, text = "Submit and Plot", command = Submitplot).pack(side =
BOTTOM)

Here, the output comes as:
[image: Inline image 1]
The problem is I am not able to use grid layout instead of pack (used
here). I want to place the text to the left, top side in the window.

Also, when I hover the mouse over the figure and the x and y values are
shown, it flickers really fast and the white space in the Tkinter window
turns black when it flickers. Can someone please tell me what the issue
could be? Also, the toolbar should have been at the bottom but it shows up
at the top.

I am using pack in this Toplevel of the main root whereas in the rest of
the code, I am using grid layout. I switched to pack for this
snipet because I could not figure out how grid would work for this part.

Can someone please let me know how to correct these issues?
Thankyou so much.

Pooja
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list