Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

like image 495
K. Soerensen Avatar asked Nov 29 '18 12:11

K. Soerensen


People also ask

What does PLT axis () do?

The plt. axis() method allows you to set the x and y limits with a single call, by passing a list which specifies [xmin, xmax, ymin, ymax] : In [11]: plt.

How do I annotate in Matplotlib?

Annotating with Arrow. The annotate() function in the pyplot module (or annotate method of the Axes class) is used to draw an arrow connecting two points on the plot. This annotates a point at xy in the given coordinate ( xycoords ) with the text at xytext given in textcoords .


1 Answers

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 the documentation, 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:

enter image description here

like image 99
DavidG Avatar answered Oct 12 '22 11:10

DavidG