Using different colours in a quiver plot
Mostra commenti meno recenti
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:

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 Commento
Karthik Nagarajan
il 6 Ago 2019
Risposte (1)
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 Commenti
Adam Danz
il 6 Ago 2019
That solves the duplicate arrows problem but that doesn't solve the problem of having two different scales. It's not a big difference in this image but it might be a bigger problem with other data.
infinity
il 7 Ago 2019
You are right. This answer is just appropriate for the case of different color. However, for the case of different scale, someone else might give another solution.
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.
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).
Karthik Nagarajan
il 8 Ago 2019
Adam Danz
il 8 Ago 2019
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
Categorie
Scopri di più su Display and Presentation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!