Slowat_Kela
Slowat_Kela

Reputation: 1511

Pyplot: Change line style to arrow

I wrote this code, which makes a multi-line plot:

import numpy as np
from matplotlib.pylab import plt

test1 = [0,4]
test2 = [0,6.7]
test3 = [0,8.8]
test4 = [0,4.4]
test5 = [0,7.6]
test6 = [0,33.5]
test7 = [0,21]
test8 = [0,17.65]


# Plotting functionality starts here
plt.figure(figsize=(16,12),dpi=500)
plt.plot(test1,label='test1',color='b',linewidth=4)
plt.plot(test2,label='test2',color='g',linewidth=4)
plt.plot(test3,label='test3',color='r',linewidth=4)
plt.plot(test4,label='test4',color='c',linewidth=4)
plt.plot(test5,label='test5',color='m',linewidth=4)
plt.plot(test6, label='test6',color='dimgray',linewidth=4)
plt.plot(test7, label='test7',color='peru',linewidth=4)
plt.plot(test8, label='test8',color='blueviolet',linewidth=4)


plt.ylabel('Rate',fontsize=14,fontweight='bold')
plt.xlabel('Year',fontsize=14,fontweight='bold')
plt.title('Growth',fontsize=14,fontweight='bold')
#plt.legend(loc='upper left',bbox_to_anchor=(1,1),fontsize=14)
plt.xticks([0,1],['Year1','Year2'],fontweight='bold')
plt.yticks(fontsize=16)
#plt.xticks([])
plt.tight_layout(pad=6.0)

I want each line to have an arrow pointing out of it at the end of the line. I have seen a bunch of similar questions on SO that are saying to use quiver, or some other method. Is there no simple command to add to 'plt.plot(test1,label='test1',color='b',linewidth=4)' like 'arrow=True' or something? If so, could someone show me with an example please?

(I'm not strong enough to understand how to implement quiver into this; if someone could show me how to implement quiver either for this example that would be great).

Upvotes: 0

Views: 937

Answers (1)

Jiadong
Jiadong

Reputation: 2072

ax.annotate() is preferred over ax.arrow(), the resulting arrow of arrow() is affected by the axes aspect ratio and limits.

import numpy as np
from matplotlib.pylab import plt




# Plotting functionality starts here
fig, ax = plt.subplots(1, 1, figsize=(7.2, 7.2))

ax.annotate("", xy=(1, 10), xytext=(0, 0),arrowprops=dict(arrowstyle="->"))
ax.annotate("", xy=(1, 5), xytext=(0, 0),arrowprops=dict(arrowstyle="->"))
ax.annotate("", xy=(1, 6), xytext=(0, 0),arrowprops=dict(arrowstyle="->"))

plt.xticks([0,1],['Year1','Year2'],fontweight='bold')
plt.yticks(fontsize=16)
plt.ylim(0, 35)
plt.xlim(0, 1)

enter image description here

Upvotes: 1

Related Questions