I am annotating a plot with matplotlib with this code
for position, force in np.nditer([positions, forces]):
plt.annotate(
"%.3g N" % force,
xy=(position, 3),
xycoords='data',
xytext=(position, 2.5),
textcoords='data',
horizontalalignment='center',
arrowprops=dict(arrowstyle="->")
)
It works fine. But if I have elements at the same position, it will stack multiple arrows on each other, i.e. if I have positions = [1,1,4,4] and forces = [4,5,8,9], it will make two arrow at position 1 and two arrows at position 4, on top of each other. Instead, I want to sum the forces and only create one arrow at position 1 with force 4+5=9 and one arrow at position 4 with force 8+9=17.
How can I do this with Python and NumPy?
Edit
I guess it could be something like
import numpy as np
positions = np.array([1,1,4,4])
forces = np.array([4,5,8,9])
new_positions = np.unique(positions)
new_forces = np.zeros(new_positions.shape)
for position, force in np.nditer([positions, forces]):
pass
np.sum(array)but that's clearly not what this is about.