2

Is it possible to you this arrow in 3d?

import matplotlib.patches as patches
style="Simple,tail_width=0.5,head_width=4,head_length=8"
kw = dict(arrowstyle=style, color=b)
a3 = patches.FancyArrowPatch((0, 0), (99, 100),connectionstyle="arc3,rad=-0.3", **kw, lw=cfg._sections['styles'].get('arrows'))

I tried:

import matplotlib.patches as patches
style="Simple,tail_width=0.5,head_width=4,head_length=8"
kw = dict(arrowstyle=style, color=b)
a3 = patches.FancyArrowPatch((0, 0), (1, 1), (0.5, 1.5),connectionstyle="arc3,rad=-0.3", **kw, lw=cfg._sections['styles'].get('arrows'))

The error was:

    a3 = patches.FancyArrowPatch((0, 0), (1, 1), (0.5, 1.5),connectionstyle="arc3,rad=-0.3", **kw, lw=1)
  File "/home/linux/.local/lib/python3.6/site-packages/matplotlib/patches.py", line 4068, in __init__
    raise ValueError("either posA and posB, or path need to provided")
ValueError: either posA and posB, or path need to provided

Or at least to place the head of arrow in a point in 3d plot?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Moe
  • 301
  • 2
  • 11

1 Answers1

4

You need a custom function for this. You can use something like this:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.patches import FancyArrowPatch
import numpy as np
from mpl_toolkits.mplot3d import proj3d

class myArrow3D(FancyArrowPatch):
    def __init__(self, xs, ys, zs, *args, **kwargs):
        FancyArrowPatch.__init__(self, (0, 0), (0, 0), *args, **kwargs)
        self._verts3d = xs, ys, zs

    def draw(self, renderer):
        xs3d, ys3d, zs3d = self._verts3d
        xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
        self.set_positions((xs[0], ys[0]), (xs[1], ys[1]))
        FancyArrowPatch.draw(self, renderer)


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim([0, 2])
ax.set_ylim([2, 0])
ax.set_zlim([0, 2])
a = myArrow3D([0, 0.7], [0, 0.5], [1, 1], mutation_scale=25, lw=1.5, arrowstyle="simple", color="b")
ax.add_artist(a)
plt.show()

Source: https://stackoverflow.com/a/22867877/5025009

enter image description here

seralouk
  • 30,938
  • 9
  • 118
  • 133
  • Thank you very much and is it possible to do the same design of the head of arrow? I mean: `style="Simple,tail_width=0.5,head_width=4,head_length=8"` – Moe Nov 17 '19 at 18:02
  • 1
    see my updated answer. also consider accepting and upvoting my answer – seralouk Nov 17 '19 at 18:23
  • not sure if this is a coincidence or not, but this answer seems very closely related to another, older, answer here: https://stackoverflow.com/questions/22867620/putting-arrowheads-on-vectors-in-matplotlibs-3d-plot – tmdavison Sep 17 '21 at 15:22