MATLAB Answers

0

Using different colours in a quiver plot

Asked by Karthik Nagarajan on 6 Aug 2019
Latest activity Commented on by Adam Danz
on 8 Aug 2019
Dear all,
I am creating a 2D quiver plot and I need to show some of the arrows in the figure in a different colour from the rest. To illustrate the issue, I will use the example given on quiver documentation page (https://www.mathworks.com/help/matlab/ref/quiver.html).
Suppose I plot a quiver plot with custom scaling as follows:
clear;clc;clf;
close all hidden;
[x,y] = meshgrid(0:0.2:2,0:0.2:2);
u = cos(x).*y;
v = sin(x).*y;
figure;
quiver(x,y,u,v,1.5)
Now let us say that I want all arrows for which the vector magnitude is greater than 1.0 to be shown in red. I have tried to add the following code:
hold on;
w=sqrt(u.^2+v.^2);
bk=find(w>1.0);
quiver(x(bk),y(bk),u(bk),v(bk),1.5,'color','r')
The result is as shown below:
quiver_test.jpg
Clearly, the second set of arrows have been scaled differently from the first set. I could work around this by manually adjusting the scale value in the second quiver command but this involves painstaking trial and error. May I know if there is a better solution?
Best,
Karthik.

  1 Comment

I should mention that I am using Matlab R2013A Student version.

Sign in to comment.

Tags

Products


Release

R2013a

1 Answer

Answer by infinity
on 6 Aug 2019
Edited by infinity
on 6 Aug 2019

Hello,
My suggestion is that you plot the first figure for the vectors that have magnitude less than 1.0 (using blue color) then you plot second figure for the rest of vectors (using red color).

  5 Comments

I fiddled with it on the day the question appeared in the forum but I couldn't get it to work either so I'm looking forward to a solution, too.
@Karthik Nagarajan , please follow-up if you found a solution.
AutoScale and AutoScaleFactor are among the properties of a quiver object but I couldn't manage to manipulate them in a way that solves this problem (r2019a).
@ infinity and Adam Danz : Thank you for your response.
Unfortunately, infinity's answer doesn't quite solve my problem for the reason mentioned by Adam. I don't necessarily mind plotting a duplicate set of arrows as long as I can get them to overlap perfectly with the original set. Therefore, the main issue here is that of having two different scales. As Adam mentioned, there doesn't seem to be a way of manipulating the properties of a quiver object to achieve the desired effect. Therefore, I have explored the possiblilty of editing quiver.m.
The following code segment appears to control the autoscaling behaviour of quiver.m:
if autoscale,
% Base autoscale value on average spacing in the x and y
% directions. Estimate number of points in each direction as
% either the size of the input arrays or the effective square
% spacing if x and y are vectors.
if min(size(x))==1, n=sqrt(numel(x)); m=n; else [m,n]=size(x); end
delx = diff([min(x(:)) max(x(:))])/n;
dely = diff([min(y(:)) max(y(:))])/m;
del = delx.^2 + dely.^2;
if del>0
len = sqrt((u.^2 + v.^2)/del);
maxlen = max(len(:));
else
maxlen = 0;
end
if maxlen>0
autoscale = autoscale*0.9 / maxlen;
else
autoscale = autoscale*0.9;
end
u = u*autoscale; v = v*autoscale;
end
One option could be to modify this to something like
if autoscale,
autoscale = autoscale*0.9;
u = u*autoscale; v = v*autoscale;
end
and adjust the last argument value to quiver appropriately. However, when I attempted this, I obtained an error message saying that the function 'quiverparseargs' could not be found. There is a parseargs function in quiver.m but I am not sure why it is commented out.
Instead of editing quiver.m, you should make a copy of that function, give it a unique name, and use that instead of quiver.m. Never manipulate a built-in matlab file.
When I open quiver.m using r2019a, try quiverparseargs() is called on line 41.
pvpairs = quiverparseargs(args);
catch ME
throw(ME)
end

Sign in to comment.