Is it possible to plot a vector arrow using quiver( ), then, negate the vector so it points in the opposite direction, but then add arrowhead in original direction?

Hi there!
This is somewhat embarassing, but I've been trying this little thing for a while now, so I figured it's a good time to ask now:
I would like to plot a velocity vector, e.g. representing a fluid flow, pointing at / acting on an object (let's say, a thin rectangle).
But if I use quiver( ) to plot the vector, the vector would emanate from the object, and consequently point away from the object, not towards it.
So, my little hack was this:
  1. Plot the vector using quiver; it'll emanate from the object and point away from the object;
  2. Negate the vector, so it emanates from the object, but now it points in the opposite direction; and
  3. Remove the arrowahead from the vector.
Removing the arrowhead from the vector makes the line potentially look like it's pointing toward the object, which is what I want.
But now, here's the hard part, Matlab doesn't appear to be able to add an arrowhead, so that the vector can point in the original direction, which is what I want, so that the vector looks like it's pointing at the object. Matlab appears to be able to only add an arrowhead in the direction that the vector values indicate.
Basically, how can I add an arrowhead at the tail of the vector, but have it point in the opposite direction?
Is there a better alternative?
Thanks in advance,

1 Comment

As always, it would be helpful if you would attach enough code/data to give us a working starting-off point...

Sign in to comment.

 Accepted Answer

Perhaps you could share a working example?
When I negate U and V, the arrows point the opposite direction. I also modified the Alignment property so the arrows are generally in the same place.
tiledlayout(1,2)
nexttile
[X,Y] = meshgrid(0:6,0:6);
U = 0.25*X;
V = 0.5*Y;
quiver(X,Y,U,V,0)
title('Normal')
nexttile
[X,Y] = meshgrid(0:6,0:6);
U = 0.25*X;
V = 0.5*Y;
quiver(X,Y,-U,-V,0,'alignment','Head')
title('Negated')

11 Comments

Hi Cris!
Yes, I am aware of this workflow. Here's my "issue":
My velocity vectors are supposed to represent fluid flow pointing up and to the left, pointing up at, say, a thin rectangle / winglike object. Now, if I use quiver, I don't know a good position to place these vectors. But scientific theory tells me where the fluid flow should act on the object, so I know that much. So, let's say the position on the object where the fluid flow acts on is just (0,0), the object's geometric centroid. I am plotting the fluid velocity vector at (0,0), using quiver( ). But, it'll emanate from (0,0) and point up and to the left, when what I really want, visually, is to have the flow be beneath the object, pointing up and to the left at it. So, my hack was to negate the vector, and remove the arrowhead. And the last step, which Matlab can't seem to do: Put an arrowhead at the tail of this negated vector, and have it point in the normal, unnegated direction -- i.e., point at the object. But an arrowhead on such a vector will necessarily point in the negated, new direction, it seems.
Does that make sense?
By the way, all of this is trivial to do in Adobe Illustrator, where I finish my Matlab plots. But, for working efficiently (running thousands of numerical experiments in Matlab), I am trying to avoid usage of Illustrator and am trying to automate all plotting within Matlab.
I look forward to your response.
Thanks!
It sounds like a situation where it would be helpful to see what you mean. Can you create a simple example to demonstrate?
Based on what you said, it seems like I could create the plot by negating Y and U.
[X,Y] = meshgrid(0:6,0:6);
U = 0.25*X;
V = 0.5*Y;
quiver(X,-Y,-U,V,0,'Alignment','Head')
title('Negated')
This example is more like what you describe. Originally the arrows point up and to the left above but emanating from (0,0). By negating X and Y, now the arrows are below (0,0) but point up and to the left towards it.
tiledlayout(1,2)
nexttile
[X,Y] = meshgrid(-6:0,0:6);
U = 0.25*X;
V = 0.5*Y;
quiver(X,Y,U,V,0)
title('Original - Up and to left')
nexttile
quiver(-X,-Y,U,V,0,'alignment','Head')
title('Negated - Up and to left')
But, @Cris LaPierre, can you put the head of the arrow at (0,0)?
I think @Noob needs to compute a length of the velocity vectors and subtract from the coordinates of the object to plot, but I don't know how to relate the two--the geometry and the velocity.
That's where I think we need a MWE (minimum working example) as starting point as well...I can envision, but don't have the time to try to invent a realistic example.
One real hassle with MATLAB or I'd suggest the annotation arrow instead, but it only knows about normalized, not data positions, so trying to generalize it to locate in relationship to the pieces drawn in the axes is a real pain...
Hi dpb and Cris!
I have a 4 am wake-up time for Christmas Eve, so I actually might have to be in bed soon!
Will report back sometime tomorrow.
Thanks again for your responses!
Yes, the arrowhead can be placed at (0,0).
quiver(0,0,-0.75,0.8,'Alignment','Head')
Hi Cris!
Your solution worked!!!
And, it works without negating the vector.
What exactly is 'alignment', 'head', doing? This seems to be the solution.
I've been stuck at this for a while, so thanks a lot.
Is this alignment command a new feature?
Thanks again, and goodnight!
The alignment property for the quiver arrow is a newly-added feature with R2022a that does precisely what is needed here; it interprets the place with respect to the given coordinates from which the arrows are to be drawn. There's a good example in the doc for the later releases; I'm at R2021b here still so wasn't aware of it.
Each quiver arrow is connected to an (X,Y) point. You provide those points as the first 2 inputs to the quiver function. Alignment determines which part of the arrow is touching that point. It can be 'Tail', which is the default, 'Center' or 'Head'
Indeed...an extremely useful enhancement that solves the problem internally...one wonders why it wasn't an initial feature, but adding the enhancement certainly makes the function far more versatile...congrats to TMW!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2024b

Asked:

on 23 Dec 2024

Edited:

on 24 Dec 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!