python - Prints value when clicked on but line isn't created? -
i'm learning matplotlib library on python 3.4. i'm practicing on code user supposed interact plot clicking on it. when click happens somewhere on plot 2 things supposed happen: print out y value , create horizontal line in plot click happened.
what i'm getting when click somewhere on plot print happens desired horizontal line shows if press f (which fulls screen plot). in other words, line created when click on plot doesn't appear untill press f. ideas why it's happening?
the code:
import matplotlib.pyplot plt import numpy np fig = plt.figure() ax = fig.add_subplot(111) ax.plot(np.random.rand(10)) def pick(event): plt.hlines(event.ydata,event.xdata- 0.2,event.xdata+0.2,colors='r',linestyle='solid') print('y coord = %f'%event.ydata) fig.canvas.mpl_connect('button_press_event',pick) plt.show()
you need use interactive mode update plot after each click. use plt.ion() before plt.show().
Comments
Post a Comment