[Tutor] Matplotlib in Tkinter

Pooja Bhalode poojabhalode11 at gmail.com
Mon Feb 20 09:54:41 EST 2017


Hi,

Another issue, in this that is happening  which is far more important from
the perspective that I am looking at, is that when I click the button
twice, two graphs get created one below the other.

I tried adding a delete("all") statement as shown below.
Code:
      def Submitplot():
print "Create plot"
print concsim.get()
f = Figure(figsize = (5,5), dpi = 80)
canvas = FigureCanvasTkAgg(f, simroot)
canvas.get_tk_widget().delete("all")
a = f.add_subplot(111)
a.plot([1,2,3,4,5],[10,11,12,14,15])
canvas.show()
canvas.get_tk_widget().pack(side = TOP)

toolbar = NavigationToolbar2TkAgg(canvas, simroot)
toolbar.update()
canvas._tkcanvas.pack(side = TOP)

Button(simroot, text = "Submit and Plot", command = Submitplot).pack(side =
TOP)

But this still does not clear the earlier made canvas and creates another
graph at the bottom. Can someone please tell me what I am doing wrong here?
I would really appreciate it.
I have also added the image as to how the two plots look like.
[image: Inline image 1]

Thankyou


On Sun, Feb 19, 2017 at 2:36 PM, Pooja Bhalode <poojabhalode11 at gmail.com>
wrote:

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


More information about the Tutor mailing list