Home > Back-end >  How to draw lines between mouseclicks on a matplotlib plot?
How to draw lines between mouseclicks on a matplotlib plot?

Time:02-02

enter image description here

and this textual output:

{'event': array([[ 5.83720666e 00, -5.73988654e-01],
       [ 2.46956149e 01, -1.41575199e-02],
       [ 5.20403030e 01,  5.70227612e-01],
       [ 8.55139728e 01,  7.56837990e-01],
       [ 1.30302686e 02,  3.73795635e-01],
       [ 1.69433877e 02, -2.40054293e-01],
       [ 2.01493167e 02, -5.05237462e-01]])}

which I can use to produce this plot:

enter image description here

with this code:

import numpy as np
import matplotlib.pyplot as plt
from mpl_point_clicker import clicker

fig, ax = plt.subplots(constrained_layout=True)
ax.plot(np.sin(np.arange(200)/(5*np.pi)))
klicker = clicker(ax, ["event"], markers=["x"])

data = \
np.array([[ 5.83720666e 00, -5.73988654e-01],
         [ 2.46956149e 01, -1.41575199e-02],
         [ 5.20403030e 01,  5.70227612e-01],
         [ 8.55139728e 01,  7.56837990e-01],
         [ 1.30302686e 02,  3.73795635e-01],
         [ 1.69433877e 02, -2.40054293e-01],
         [ 2.01493167e 02, -5.05237462e-01]])

plt.plot(data[:,0],data[:,1],c='r')


plt.show()

print(klicker.get_positions())

However, I would like to draw this line as I am clicking on the plot. Ie instead of points drawn when I click on the graph on the first plot, I would like to have a line drawn between the points I click. I'd like the end result look similar to my second plot, but without needing a second script to create that plot. If I click at some new place on the plot, the red line should extend to this new location. As text output, I would like to have the coordinates of my clicks, just like in the examples above.

How can I achieve this in Python?

CodePudding user response:

The enter image description here

Other Line2D keywords can be found here.

  •  Tags:  
  • Related