FootyStattowannab3
FootyStattowannab3

Reputation: 41

Arrows on Scatter

Having had no luck myself I'm looking for a bit of assistance with this problem I have run into.

I have CSV data in this format.

Flow Amount,X_Start,Y_Start,X_End,Y_End
4,36.20740741,31.97407407,48.103125,9.4125
4,50.71851852,48.46666667,65.65714286,73.81785714
3,50.71851852,48.46666667,48.103125,9.4125
3,48.103125,9.4125,62.14642857,22.55357143
2,31.976,71.308,40.19210526,88.43157895
1,56.98888889,48.18333333,36.20740741,31.97407407
1,56.98888889,48.18333333,50.71851852,48.46666667
0,46.65686275,72.46470588,48.103125,9.4125
0,65.65714286,73.81785714,12.18333333,55.78333333

I am looking to plot an arrow onto my scatter from the points at (X_Start, Y_Start) to the point (X_End, Y_End) with an arrow head at the End point.

I would also like to change the weight of the arrow based upon column 1 [Flow] showing a thicker line, the greater the number.

I have created a plot with everything except the arrows and the arrow effect I am looking to achieve is much like the one from the screenshot.

enter image description here

I've looked into plt.quiver() but that only takes 1 point so I'm looking for an effective workaround to add these arrows to my plot while I loop through the CSV data.

I would be thankful for any solution ideas.

Upvotes: 3

Views: 9856

Answers (1)

Serenity
Serenity

Reputation: 36635

You can use 'arrow' to plot arrow, like here:

import numpy as np
from matplotlib import pyplot as plt

data = np.genfromtxt('file1.dat', delimiter=',', skip_header=1, names=['MAG', 'X0', 'Y0','X1','Y1'])
plt.scatter(data['X0'], data['Y0'], color='r', zorder=10)
plt.scatter(data['X1'], data['Y1'], color='r', zorder=10)

for d in data:
    if(d['MAG'] > 0):
        plt.arrow(d['X0'],d['Y0'],d['X1']-d['X0'], d['Y1']-d['Y0'], 
         shape='full', color='b', lw=d['MAG']/2., length_includes_head=True, 
         zorder=0, head_length=3., head_width=1.5)

plt.show()

enter image description here

Another way you have to use FancyArrow, like here:

for d in data:
    if(d['MAG'] > 0):
        ar = patches.FancyArrow(d['X0'],d['Y0'],d['X1']-d['X0'], d['Y1']-d['Y0'], width=d['MAG']/4.,
         head_width=3., head_length=1.54, length_includes_head=True, zorder = 0) 
        plt.gca().add_patch(ar)

Upvotes: 3

Related Questions