python - Plot hlines in embedded graph? -


i'm practicing tkinter , matplotlib in piece of code , supposed embed simple matplotlib graph tkinter gui. thing want horizontal lines plotted in embedded graph mouse clicks i'm not getting result want.

what i'm getting first mouse click new plot created outside tkinter gui of same graph 1 horizontal line in (where clicked on). if close plot window popped , try click again on embedded graph new plot pops time contains horizontal line.

what's going on?

the code i'm practicing on:

from tkinter import * import matplotlib matplotlib.use('tkagg') matplotlib import pyplot plt matplotlib.backends.backend_tkagg import figurecanvastkagg, navigationtoolbar2tkagg matplotlib.figure import figure  root = tk()  frame = frame(root) frame.pack() fig = plt.figure(figsize=(16,8)) plt.plot([1,2,3],[1,2,3]) canvas = figurecanvastkagg(fig, frame) canvas.show() canvas.get_tk_widget().pack(fill='both', expand=true) toolbar = navigationtoolbar2tkagg(canvas, frame) toolbar.update() canvas._tkcanvas.pack(fill='both', expand=true)  def pick(event):     plt.ion()     plt.hlines(event.ydata,event.xdata-0.009,event.xdata+0.009,colors='red',            linestyle='solid') fig.canvas.mpl_connect('button_press_event', pick) 

instead of using plt.ion(), call canvas.show() after drawing line.


moreover, following matplotlib embedding in tk example, wouldn't use pyplot use figure (which have imported don't use). code become:

from tkinter import * import matplotlib matplotlib.use('tkagg') matplotlib.backends.backend_tkagg import figurecanvastkagg, navigationtoolbar2tkagg matplotlib.figure import figure  root = tk()  frame = frame(root) frame.pack() fig = figure(figsize=(16,8)) p = fig.add_subplot(1,1,1) p.plot([1,2,3],[1,2,3]) canvas = figurecanvastkagg(fig, frame) canvas.show() canvas.get_tk_widget().pack(fill='both', expand=true) toolbar = navigationtoolbar2tkagg(canvas, frame) toolbar.update() canvas._tkcanvas.pack(fill='both', expand=true)  def pick(event):     p.hlines(event.ydata, event.xdata-0.009, event.xdata+0.009,              colors='red', linestyle='solid')     canvas.show()  fig.canvas.mpl_connect('button_press_event', pick)  root.mainloop() 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -