0

Hello I have a code and I would like to display my two graphs on the same figure. Here is my code :

import Tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np

app = tk.Tk()
app.wm_title("Graphs")

fig = Figure(figsize=(6, 4), dpi=96)
a = np.array([1,2,3])
ax = fig.add_subplot(111)
bx = fig.add_subplot(111)
bx.plot(a,np.array([0,0.5,2]))


graph = FigureCanvasTkAgg(fig, master=app)
canvas = graph.get_tk_widget()
canvas.grid(row=0, column=0, rowspan = 11, padx =10, pady =5)

def updateScale(value):
   ax.clear()
   print "scale is now %s" % (value)
   #a = np.array([1, 2, 3])
   b = float(value)*a
   ax.plot(a,b)
   graph.show()

value = tk.DoubleVar()
scale = tk.Scale(app, variable=value, orient="horizontal",length = 100, from_=0.55, to=2.75, resolution = 0.01,command=updateScale)
scale.grid(row=0, column=1)

app.mainloop()

Everything is okay except that I don't watch bx... Any suggestions ? Thank you !

j_4321
  • 15,431
  • 3
  • 34
  • 61
  • Have a look [here](https://stackoverflow.com/questions/45328966/plotting-multiple-graphs-in-separate-tkinter-figures-within-the-same-window) - you can add multiple frames into one windows and you can use each for a graph. it´s with an example. – LenglBoy Oct 25 '17 at 12:47
  • https://stackoverflow.com/questions/3584805/in-matplotlib-what-does-the-argument-mean-in-fig-add-subplot111#11404223 also take a look here, looks like you add both sub-plots with (111) which maybe leads to overlapping – voiDnyx Oct 25 '17 at 12:52
  • Actually I want these two plots in the same subplot. But the problem is in the function updateScale(value) I have ax.clear() and I just want to clear plot(a,b) –  Oct 25 '17 at 12:54

1 Answers1

1

You don't want to use two different subplots here. Instead you can take the same axes to plot to. Then don't use clear, since it destroys the initial graph. Instead only update an existing line. If line, = axp.plot(), line.set_data() will set new data to the line. Then rescale the axes and draw the canvas.

import Tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np

app = tk.Tk()
app.wm_title("Graphs")

fig = Figure(figsize=(6, 4), dpi=96)
a = np.array([1,2,3])
ax = fig.add_subplot(111)

line, = ax.plot(a,np.array([0,0.5,2]))
line2, = ax.plot(a,0.55*a)

graph = FigureCanvasTkAgg(fig, master=app)
canvas = graph.get_tk_widget()
canvas.grid(row=0, column=0, rowspan = 11, padx =10, pady =5)

def updateScale(value):
   print "scale is now %s" % (value)
   b = float(value)*a
   # set new data to the line
   line2.set_data(a,b)
   # rescale the axes
   ax.relim()
   ax.autoscale()
   #draw canvas
   fig.canvas.draw_idle()


value = tk.DoubleVar()
scale = tk.Scale(app, variable=value, orient="horizontal",length = 100, 
                 from_=0.55, to=2.75, resolution = 0.01,command=updateScale)
scale.grid(row=0, column=1)

app.mainloop()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712