Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show direction arrows in a scatterplot

I am plotting data in a scatterplot an I would like to see the direction of the hysteresis. Does anyone have a good idea how to implement arrows on each line that point into the direction of the next point?

Alternatively, the markers could be replaced by arrows pointing in the direction of the next point.

What I am looking for:

enter image description here

Code to obtain the plot (without arrows):

df = pd.DataFrame.from_dict({'x' : [0,3,8,7,5,3,2,1],
                             'y' : [0,1,3,5,9,8,7,5]})
x = df['x']
y = df['y']
fig, ax = plt.subplots()
ax.scatter(x,y)
ax.plot(x,y)
like image 811
NicoH Avatar asked Aug 25 '26 23:08

NicoH


1 Answers

As commented, one can use plt.quiver to produce arrows along a line, e.g. like

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame.from_dict({'x' : [0,3,8,7,5,3,2,1],
                             'y' : [0,1,3,5,9,8,7,5]})
x = df['x'].values
y = df['y'].values

u = np.diff(x)
v = np.diff(y)
pos_x = x[:-1] + u/2
pos_y = y[:-1] + v/2
norm = np.sqrt(u**2+v**2) 

fig, ax = plt.subplots()
ax.plot(x,y, marker="o")
ax.quiver(pos_x, pos_y, u/norm, v/norm, angles="xy", zorder=5, pivot="mid")
plt.show()

enter image description here

like image 82
ImportanceOfBeingErnest Avatar answered Aug 27 '26 14:08

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!