Plot linestyle as an arrow

I have tried to search for the answer but have not found anything useful so forgive me if this has been answered before.
I want to change the linestyle of a regular plot to an arrow, in the direction of the data in the tuple. So in this script below I would like an arrow head pointing to the next marker.
There does not seem to be a line style to do it, but is there a way I can add the head at the correct end of the line?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

labels=(‘91’,‘92’,‘93’,‘94’,‘95’)
data1 = (2500, 3000, 3500, 3000, 2700)
data2 = (200, 250, 270, 320, 250)

fig = plt.figure()
ax = fig.add_subplot(111)
p = ax.plot(data1, data2, ‘bo’, linestyle=’-’, markersize=3)
ax.set_ylim(0, 350)

for i, label in enumerate(labels):
x_loc = data1[i]
y_loc = data2[i]
txt = ax.annotate(label, xy=(x_loc, y_loc), size=8,
xytext=(-10, 10), textcoords=‘offset points’,
arrowprops=None)

plt.show()

Catherine

Catherine Thwaites, on 2011-03-23 10:10, wrote:

I have tried to search for the answer but have not found anything useful so
forgive me if this has been answered before.
I want to change the linestyle of a regular plot to an arrow, in the
direction of the data in the tuple. So in this script below I would like an
arrow head pointing to the next marker.
There does not seem to be a line style to do it, but is there a way I can
add the head at the correct end of the line?

Hi Catherine,

here's one way to get the type of plot you desire, and you might
also consider using a series of annotation with arrows to get a
similar effect:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

labels=('91','92','93','94','95')
data1 = np.array([2500, 3000, 3500, 3000, 2700])
data2 = np.array([200, 250, 270, 320, 250])

fig = plt.figure()
ax = fig.add_subplot(111)
# the next line is only use to set the proper lims,
p = ax.plot(data1, data2, 'bo', linestyle='-', markersize=3, alpha=0)
for x,y,dx,dy in zip(data1[:-1],data2[:-1], np.diff(data1), np.diff(data2)):
    ax.arrow(x,y,dx,dy, head_width=10, length_includes_head=True)
ax.set_ylim(0, 350)

for i, label in enumerate(labels):
    x_loc = data1[i]
    y_loc = data2[i]
    txt = ax.annotate(label, xy=(x_loc, y_loc), size=8,
        xytext=(-10, 10), textcoords='offset points',
        arrowprops=None)

plt.axis('equal')
plt.show()

best,

···

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7

Thanks for your help, I simplified what I was doing and so changed the data, which is my fault. Here is some data of similar size to the data I am plotting:
data1 = np.array([22000, 25000, 27000, 32000, 25000])

data2 = np.array([14000000, 22000000, 3500000, 3000000, 2700000])

When I try to run this with the above code I get a very strange graph and these errors:

In [16]: run t.py
Warning: overflow encountered in long_scalars
Warning: overflow encountered in long_scalars
Warning: overflow encountered in long_scalars
Warning: overflow encountered in long_scalars
Warning: invalid value encountered in sqrt

So then tried the same method using the annotation as suggested (code below) which worked perfectly.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.ticker import FuncFormatter

labels=(‘91’,‘92’,‘93’,‘94’,‘95’)

data1 = np.array([22000, 25000, 27000, 32000, 25000])
data2 = np.array([14000000, 22000000, 3500000, 3000000, 2700000])

fig = plt.figure()

ax = fig.add_subplot(111)

the next line is only use to set the proper lims,

p = ax.plot(data1, data2, ‘bo’, linestyle=’-’, markersize=3, alpha=0)
for x,y,dx,dy in zip(data1[:-1], data2[:-1], data1[1:], data2[1:]):

ax.annotate(’’, xy=(dx,dy), xycoords=‘data’,
xytext=(x,y), textcoords=‘data’,
arrowprops=dict(arrowstyle="->"))

ax.set_xlim(21000,33000)

for i, label in enumerate(labels):
x_loc = data1[i]
y_loc = data2[i]
txt = ax.annotate(label, xy=(x_loc, y_loc), size=8,
xytext=(-10, 10), textcoords=‘offset points’,
arrowprops=None)

plt.show()

Just in case someone else has a similar problem.

Thanks
Catherine