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: 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?
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')
resolution of the figure. If not provided, defaults to rcParams["figure. dpi"] (default: 100.0) = 100 .
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With