Skip to content Skip to sidebar Skip to footer

Plot Vertical Arrows For My Points

I am trying to figure out a way to add a vertical arrow pointing up for each of my data points. I have scatter plot and code below. I need the vertical arrows to start from the poi

Solution 1:

This is bit hacky, adjust arrow_offset and arrow_size until the figure looks right.enter image description here

import matplotlib.pyplot as plt
fig = plt.figure()
a1 = fig.add_subplot(111)

simbh = np.array([5.3,  5.3,  5.5,  5.6,  5.6,  5.8,  5.9,  6.0,   6.2,  6.3,  6.3])
simstel =np.array([10.02, 10.08, 9.64, 9.53, 9.78, 9.65, 10.05, 10.09, 10.08, 10.22, 10.42])
sca2=a1.scatter(simstel, simbh, c='w' )
arrow_offset = 0.08
arrow_size = 500
sca2=a1.scatter(simstel, simbh + arrow_offset, 
                marker=r'$\uparrow$', s=arrow_size)

Solution 2:

This can be done directly

from matplotlib import pyplot as plt
import numpy as np

# set up figure
fig, ax = plt.subplots()

# make synthetic data
x = np.linspace(0, 1, 15)
y = np.random.rand(15)
yerr = np.ones_like(x) * .2# if you are using 1.3.1 or older you might need to use uplims to work# around a bug, see below

ax.errorbar(x, y, yerr=yerr, lolims=True, ls='none', marker='o')

# adjust axis limits
ax.margins(.1)  # margins makes the markers not overlap with the edges

demo image

There was some strangeness in how these arrows are implemented where the semantics changed so that 'lolims' means 'the data point is the lower limit' and 'uplims' means 'the data point is the maximum value'.

See https://github.com/matplotlib/matplotlib/pull/2452

Solution 3:

The other approaches presented are great. I'm going for the hackiest award today:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

simbh = np.array([5.3,  5.3,  5.5,  5.6,  5.6,  5.8,  5.9,  6.0,   6.2,  6.3,  6.3])
simstel = np.array([10.02, 10.08, 9.64, 9.53, 9.78, 9.65, 10.05, 10.09, 10.08, 10.22, 10.42])
sca2 = ax.scatter(simstel, simbh)
for x, y inzip(simstel, simbh):
    ax.annotate('', xy=(x, y), xytext=(0, 25), textcoords='offset points', 
                arrowprops=dict(arrowstyle="<|-"))

enter image description here

Solution 4:

This is not super elegant, but it does the trick

to get the arrows start at the data point and go up 0.2 units:

for x,y in zip(simstel,simbh):
    plt.arrow(x,y,0,0.2)

enter image description here

Post a Comment for "Plot Vertical Arrows For My Points"