Warm tip: This article is reproduced from stackoverflow.com, please click
matplotlib python scatter-plot

Show direction arrows in a scatterplot

发布于 2020-07-01 15:49:49

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)
Questioner
NicoH
Viewed
5
ImportanceOfBeingErnest 2019-10-11 23:35

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