How To Draw Lines Between Mouseclicks On A Matplotlib Plot?
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:
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:
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?
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": "--"})
Other Line2D keywords can be found here.
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module