Ad

How To Draw Lines Between Mouseclicks On A Matplotlib Plot?

- 1 answer

This answer shows an excellent way of getting coordinates of mouse clicks on a matplotlib plot using mpl_point_clicker.

With a few clicks and 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"])

plt.show()

print(klicker.get_positions())

I am able to produce this plot:

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?

Ad

Answer

The API says: **line_kwargs (kwargs) – Line2D objects (from ax.plot) are used to generate the markers. line_kwargs will be passed through to all of the ax.plot calls.
So, let's set the linestyle:

klicker = clicker(ax, ["event"], markers=["x"], **{"linestyle": "--"})

Sample output:
enter image description here

Other Line2D keywords can be found here.

Ad
source: stackoverflow.com
Ad