I am trying to get a scatter plot of 2 different variables, one set of scatter points is height values for a projectile under the effect of air resistant, and one is a projectile with no air resistance. I can get this working with 1 set of scatterpoints, but not both of them. I'm using the mplcursors library to show annotation labels when hovering over a point. This is the relevant code:
import numpy as np
from matplotlib import pyplot as plt
import mplcursors
# ...
fig, ax = plt.subplots()
nair_scatter = ax.scatter(nairRangeValues, nairHeightValues, c="blue", label="No air resistance", s=3)
air_scatter = ax.scatter(airRangeValues, airHeightValues, c="red", label="Air resistance", s=3)
ax.legend()
plt.xlabel("Range", size=10)
plt.ylabel("Height", size=10)
crs = mplcursors.cursor(ax,hover=True)
crs.connect("add", lambda sel: sel.annotation.set_text(
'Range {} m\nHeight {} m\nVelocity {} m/s at angle {} degrees\nDisplacement {} m\nTime of flight {} s' .format(
(sel.target[0]), (sel.target[1]),
(airVelocityValues[get_index(airRangeValues, sel.target[0])]),
(airAngleValues[get_index(airRangeValues, sel.target[0])]),
(airDisplacementValues[get_index(airRangeValues, sel.target[0])]),
(airTimeValues[get_index(airRangeValues, sel.target[0])]) ) ) )
crs2 = mplcursors.cursor(ax,hover=True)
crs2.connect("add", lambda ok: ok.annotation.set_text(
'Range {} m\nHeight {} m' .format(ok.target[0], ok.target[1])))
plt.show()
There are a few problems with this. Firstly, it gives me a massive error and says StopIteration
at the end. The other one is that it shows the correct label on 1 set of scatter points, but also shows the crs2 values for the same scatterplot, not the other one. I have no idea how to allow them to be able to be unique to each scatter point set, if anyone can help id appreciate it.