简体   繁体   中英

arrow in plot matplotlib.pyplot

I am unable to get the arrow to display like I want it. Here is an example of what I am trying to do:

import matplotlib.pyplot as plt
import numpy as np

b = np.arange(5)*-1E-4
a = np.arange(5)

fig, ax = plt.subplots()
ax.plot(a,b, linewidth=3, color="k") 
plt.arrow(1,-0.00010,0,-0.00005, shape='full', lw=3, length_includes_head=True, head_width=.01)
plt.show()

As far as I understand, this should produce an arrow starting at (1,-0.00010) and ending at (1,-0.00015) But the result is a much longer line, no longer looking like an arrow, and not starting and stopping at the right points.

在此处输入图片说明

Because you are using such small scales, some arguments which you have not explicitly passed to plt.arrow , will use their defaults, which in your case will not give a nice outcome.

Looking at thedocumentation , if no value for width is passed then the default value is 0.001, then the head width will be 0.003 and the head length will be 0.0015. Because the head width is too small using the default values, and the head length is much too big you get the output seen in the question

Therefore, you need to pass in the arguments head_width and head_length :

plt.arrow(1, -0.00010, 0, -0.00005, length_includes_head=True,
          head_width=0.08, head_length=0.00002)

which gives:

在此处输入图片说明

You can draw arrow with annotate. matplotlib docs

a = np.linspace(-2,2, 100)
plt.plot(a, a**2)
plt.annotate("here", xy=(0, 0), xytext=(0, 2), arrowprops=dict(arrowstyle="->"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM