Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change arrow style of matplotlib quiver function

Tags:

matplotlib

Is there a way to change the style of the arrows of the matplotlib quiver function?

I tried passing the arrowprops=dict() kwarg to the function, but that seems to only work with annotate function.

In any case the arrow style I am looking for, does not seem to be included in matplotlib. I think they are called "half arrows". I can insert these arrows using UTF-8 characters, but the arrows can then not be customized and it is hard to get them aligned properly. Is there a better way to do this (Maybe with inserting a SVG symbol)?

enter image description here

The code of my solution above:

import matplotlib.pyplot as plt
import numpy as np
import mplstereonet
import matplotlib.image as image

#UTF-8 characters don't seem to work in the normal pyplot backend
plt.switch_backend("gtk3cairo")

fig = plt.figure()
ax = fig.add_subplot(111, projection='stereonet')

arrows = [[120, 30, 120, 30, "sin"],
          [160, 50, 160, 50, "dex"]]

for arrow in arrows:
    strike = arrow[0] - 90
    ax.plane(strike, arrow[1], color="#000000")
    ax.line(arrow[3], arrow[2], color="#ff0000")

    x, y = mplstereonet.line(arrow[3], arrow[2])
    ang = np.degrees(np.arctan2(x, y))[0] * (-1)
    gap = 0.08
    gap_x = gap * np.sin(ang)
    gap_y = gap * np.cos(ang)

    if arrow[4] == "dex":
        ax.text(x - gap_x, y - gap_y, "⇀", size=30, rotation=ang,
                horizontalalignment="center", verticalalignment="center")
        ax.text(x + gap_x, y + gap_y, "↽", size=30, rotation=ang,
                horizontalalignment="center", verticalalignment="center")

    elif arrow[4] == "sin":
        ax.text(x - gap_x, y - gap_y, "↼", size=30, rotation=ang,
                horizontalalignment="center", verticalalignment="center")
        ax.text(x + gap_x, y + gap_y, "⇁", size=30, rotation=ang,
                horizontalalignment="center", verticalalignment="center")

plt.show()

Similar questions:

  • How to use custom marker with plot?
  • Matplotlib custom marker/symbol
like image 215
tobias47n9e Avatar asked Oct 30 '22 22:10

tobias47n9e


1 Answers

Note: This is only a partial answer, about the use of arrowprops=dict().


Part of the problem as that there are 7 (!) artists that can be used to draw arrows (Arrow, FancyArrow, FancyArrowPatch, ConnectionPatch, YAArrow, SimpleArrow (which is a sub-class of FancyArrowPatch) and FilledArrow) all of which behave differently and have different interfaces.

This does not include quiver which does draw arrows, but is a PolyCollection sub-class.

You can read more on this arrows discussion here.

Also mentioned there is that some functions change the arrow style automatically, according to the parameters given:

[...] the issue with the annotate function is that it's not obvious to a user that the API changes when specifying a different arrowstyle.

I had a similar issue with annotate, which you may see in this question.

like image 190
Luis Avatar answered Jan 04 '23 14:01

Luis