mzzx
mzzx

Reputation: 2164

Matplotlib arrow in loglog plot

I'm trying to draw an arrow into a loglog plot with matplotlib, which looks like this:

enter image description here

I know that it has been suggested to turn off the axis (Matplotlib: Draw a vertical arrow in a log-log plot), but I do need the axes. In addition, the suggestion did not seem to change anything (apart from turning the axes off, as expected):

plt.figure();plt.loglog([1,10,60],[1,0.1,0.005])
plt.axis('off')
plt.arrow(2,0.002,5,0.098,'k',head_length=0.3)

My work around so far has been to create an invisible inset (meaning: axes off) with a linear axes environment and plot the arrow in the inset, which works but is really a bit unpleasant. Is there a simpler way? Or do people recommend to add these type of additional features with eg. inkscape, after the main plot is done?

Upvotes: 6

Views: 2248

Answers (1)

tmdavison
tmdavison

Reputation: 69106

You can use plt.annotate rather than plt.arrow. This is noted in the documentation for plt.arrow:

The resulting arrow is affected by the axes aspect ratio and limits. This may produce an arrow whose head is not square with its stem. To create an arrow whose head is square with its stem, use annotate()

For example:

import matplotlib.pyplot as plt

plt.figure()
plt.loglog([1,10,60],[1,0.1,0.005])
plt.annotate('', xy=(5, 0.098), xytext=(2, 0.002), 
            arrowprops=dict(facecolor='black', shrink=0.),
            )

plt.ylim(0.001, 10)

plt.show()

enter image description here

Note that you may need to adjust the axes limits to fit the arrow into the plot. Here I had to change ylim.

Upvotes: 7

Related Questions