MATLAB Answers

0

How to assign specific colors to every quiver?

Asked by Thomas Schlatter on 17 Oct 2019
Latest activity Commented on by Thomas Schlatter on 17 Oct 2019
Hi,
I would like to assign specific colors to my quiver plot. There are 13 quivers and i would assign to it 13 colors to reproduce it in a further iteration.
So my matrix A is [13x2], u and v are [13,1] and the color matrix col is [13x3].
The code should looke somehow like this but quiver doesn't accept a color matrix:
h(t)= quiver(A(:,1),A(:,2),u,v,1000,'color',col);
Is there a simple solution for this problem?
Thank you!

  0 Comments

Sign in to comment.

2 Answers

Answer by darova
on 17 Oct 2019

You can manually assign color to each vector
clc,clear
[x,y] = meshgrid(0:0.2:2,0:0.2:2);
u = cos(x).*y;
v = sin(x).*y;
h = quiver(x,y,u,v);
% exctract data of vectors
h1 = get(h,'children');
x1 = get(h1(1),'xdata'); % arrowline
y1 = get(h1(1),'ydata');
x2 = get(h1(2),'xdata'); % arrowhead
y2 = get(h1(2),'ydata');
pause(0.2)
k = 0;
hold on
for i = 1:length(x1)/3
color = rand(1,3);
ii = (1:3) + (i-1)*3;
plot(x1(ii),y1(ii),'color',color)
plot(x2(ii+k),y2(ii+k),'color',color)
k = k + 1;
end
hold off

  3 Comments

Thanks for the fast answer.
I hoped to solve this without a loop. But nevertheless I tried this solution but in my code the children of the quiver is always empty. ('0×0 empty GraphicsPlaceholder array').
H= quiver(x,y,u,v)
h1 = get(H,'children')
Do you know why this is the case? in the plot the quiver are shown correctly.
What length of H vector?
H is a '1x1 Quiver'.

Sign in to comment.


Answer by Guillaume
on 17 Oct 2019

I can't remember if quiver did have children before R2014b (when the graphics system changed drastically) but since R2014b it does not (unless it has datatips, in which case the children are just the datatips).
It's not possible to access the individual arrows in a single quiver plot, at least in a documented way, and therefore it's not possible to change the colour of individual arrows.
You're left with making a single quiver call for each arrow (which may be slow) or searching on the FileExchange for an alternative implementation, e.g. this, this or this and probably more. Note that some of these functions (e.g. the first one) may use unsupported ways of accessing the quivers, which may not work in future matlab versions.

  1 Comment

Okey, yes I use R2019a.
Thanks I'll take a look at it.

Sign in to comment.