Question

I have a set of segment lines AB. Point A is located in [x0,y0] = [0.0] coordinate as being radial point. B is the end of segment lines [x1,y1]. I found some answers here and I tried to adapt it to my problem.

The question: How can I plot radial segment showing that B is the end of arrow.? Because, in my code arrow sizes does exceed the plot window.

Any helps would be appreciated.

import numpy as np
import pylab as pl

def arrow_segment(AB):

    for idx in range(0,len(AB)):
            plt.arrow(AB[idx,0],  #x1
                  AB[idx,1],  # y1
                  AB[idx,2]-AB[idx,0], # x2 - x1
                  AB[idx,3]-AB[idx,1], # y2 - y1
                  color='blue',head_width=0.05, head_length=0.1)
    plt.show()
AB = [[0, 0., 1., 10]]
arrow_segment(AB)
Was it helpful?

Solution

My understanding is that you're asking what to do when the arrowhead is off the plot. There are a few options:

  1. You can draw the arrow up to the boundary and use a type of arrow head that indicates that it's not the true end of the arrow, such as a half arrow head, "shape"="left", or open vs. closed, etc.
  2. You can draw the arrowhead midway along the arrow, like is done for a streamline. I don't know a command for this in mpl, but you could easily do it using two arrows, or just plotting a line and the head of an arrow, etc.

Edit: For completeness, I'll add my take on a #2. Here's I plot the line, and then plot the arrow over the line, and the arrowhead can be plotted anywhere along the line and in controlled by f (between 0 and 1). The example shows a few lines plotted for f ranging from 0 to .1.

import numpy as np
import pylab as plt

def arrow_mh(v4, color, f=.5):
    x0, y0, x1, y1 = v4
    line = plt.plot([x0, x1], [y0, y1], color=color)

    f = max(f, .0001)
    dx = f*(x1-x0)
    dy = f*(y1-y0)
    a = plt.arrow(x0, y0, dx, dy,
          color=color,head_width=0.05, head_length=0.1)

def arrow_segment(AB):

    for idx in range(0,len(AB)):
        arrow_mh(AB[idx], 'blue', .01*(9-idx))
    plt.xlim(0, 1)
    plt.ylim(0, 1)
    plt.show()
AB = np.array([[.1*i, .1, .5, 4] for i in range(10)])
arrow_segment(AB)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top