Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib arrowheads and aspect ratio

If I run this script:

import matplotlib.pyplot as plt
import pylab as plab

plt.figure()
plt.plot([0,2], [2,0], color='c', lw=0.5)
plt.plot([1,2], [2,1], color='k', lw=0.5)
plt.arrow(1,1,0.5,0.5, head_width=0.1, width=0.01, head_length=0.1, color='r')
plt.arrow(1.25,0.75,0.5,0.5, head_width=0.1, width=0.01, head_length=0.1, color='g')

plab.axes().set_aspect(0.5)

plt.show()

I get this: enter image description here Notice that the backs of the arrowheads are flush with that black line that stretches from (1,2) to (2,1). That makes sense, but I want the backs of the arrowheads to be VISUALLY perpendicular to the tails WITHOUT changing the aspect ratio. How do I do this?

like image 831
Forklift17 Avatar asked Jun 14 '16 18:06

Forklift17


People also ask

What is Matplotlib aspect ratio?

Aspect ratio is the ratio of height to width of the image we want to display. Matplotlib provides us the feature of modifying the aspect ratio of our image by specifying the value for the optional aspect ratio attribute for our image plot. Syntax: pyplot.imshow(image, aspect='value')

What is Matplotlib default DPI?

resolution of the figure. If not provided, defaults to rcParams["figure. dpi"] (default: 100.0) = 100 .


1 Answers

I've been annoyed with that problem for (almost) ever, and mostly end up using annotate() to draw arrows. For example (lacking a lot of tweaking to end up with the identical result as your plot...):

import matplotlib.pyplot as plt
import pylab as plab

plt.figure()
ax=plt.subplot(211)
plt.plot([0,2], [2,0], color='c', lw=0.5)
plt.plot([1,2], [2,1], color='k', lw=0.5)
plt.arrow(1,1,0.5,0.5, head_width=0.1, width=0.01, head_length=0.1, color='r')
ax.set_aspect(0.5)

ax=plt.subplot(212)
plt.plot([0,2], [2,0], color='c', lw=0.5)
plt.plot([1,2], [2,1], color='k', lw=0.5)
ax.annotate("",
            xy=(1.5, 1.5), xycoords='data',
            xytext=(1, 1), textcoords='data',
            arrowprops=dict(arrowstyle="-|>",
                            connectionstyle="arc3"),
            )
ax.set_aspect(0.5)

plt.show()

enter image description here

like image 57
Bart Avatar answered Sep 28 '22 15:09

Bart