How to create a quiver plot with logarithmic scaled arrows

Hey, I have a vector field with a large dynamic range; therefore the only way to properly see it in a quiver plot is if the length of the vectors will scale logarithmic instead of linearly.
As far as I know, there is no built in way to do it. Manually take the log before calling quiver will not work as it will change the angles, and therefore the quiver plot will be wrong.
I tried searching online but couldn't a way to do it, anyone knows one?
Another option is starting with the matlab built in quiver plot code and manually making another function that fixes it, is there anyway to get it?

2 commentaires

Interesting problem. The code for quiver is viewable. Type
open quiver
and it should be relatively painless to manually hack the length scaling.
Thanks! Didn't know it was that simple to open the code for matlab functions :)
Unfortunately, it did not help me; taking a look at the code, the important lines are just these two:
h = matlab.graphics.chart.primitive.Quiver;
set(h,'Parent',parax,'Color_I',c,'LineStyle_I',ls,pvpairs{:});
pvpairs is simply the x,y,u,v data, so it seems that the behavior is hard-coded into a primitive "Quiver" chart type :\
Any other ideas?

Connectez-vous pour commenter.

 Réponse acceptée

I’m not certain what you’re plotting, so I’m guessing here.
This is one approach:
t = linspace(1E-3, 6*pi);
x = t .* cos(t) + 2;
y = t.* sin(t) + 2;
dx = gradient(x);
dy = gradient(y);
figure(1)
quiver(x, y, dx, dy) % Retain Scaling
grid
axis equal
log_dx = log(hypot(dx,dy)) .* (dx);
log_dy = log(hypot(dx,dy)) .* (dy);
figure(2)
quiver(x, y, log_dx, log_dy, 0) % Log Arrows, No Scaling
grid
axis equal

6 commentaires

Hey Star Strider. This is a nice trick to do it, Thanks!
It actually scaled things as log(x)*x, but doing the following produces exactly what I wanted:
log_dx = log(hypot(dx,dy).^2)./hypot(dx,dy).*(dx);
My pleasure!
I like your modification. This is useful, so I’m voting it up.
Yoav Romach
Yoav Romach le 28 Mar 2016
Modifié(e) : Yoav Romach le 28 Mar 2016
Thanks!
As a follow up, it seems that there were still some problems with the code, if some of the magnitudes (hypot(dx,dy)) were smaller than one, then the results would be messed up.
The following code works perfectly:
function PlotQuiverInLogScale(x, y, u, v, s)
%PlotQuiverInLogScale Plots matlab quiver with log scaling.
if nargin<5
s=1;
end
R = hypot(u,v);
minR = min(min(R(R~=0)));
uNorm = u./R;
vNorm = v./R;
uLog = log(R/minR).*uNorm;
vLog = log(R/minR).*vNorm;
if ~isequal(size(x),size(u))
[x, y] = meshgrid(x,y);
end
figure
quiver(x, y, uLog, vLog, s)
end
My pleasure.
Your code looks as though it could be a useful File Exchange contribution.
You already created a function from your code, so all you now need to do is to comment it appropriately, explaining in a comment what each line does (this helps others understand your code, and you to remember what you did if you need to tweak it later), make it as robust as you can (testing for out-of-range input arguments and other potential problems), and add a series of initial comments explaining how to use it (this will come up if your right-click on the function name in the editor, or type help function_name in the Command Window). Include a link to this thread in the documentation if you want to.
Then post back to this thread with its File Exchange link.
Arthur Ku
Arthur Ku le 22 Sep 2018
Modifié(e) : Arthur Ku le 22 Sep 2018
Very nice, Yoav!
I tested this out with a few point charges and it looks great when superimposed on a contour plot. Thank you both for your solutions. As Star Strider said, this would be great on File Exchange.
Yoav Romach
Yoav Romach le 15 Oct 2018
Modifié(e) : Star Strider le 15 Oct 2018
Thanks Arthur.
About two years later, I added it to file exchange with some additional commenting (but no significant code changes). If people will report bugs, I'll try to fix them.
Hopefully it'll help people :)

Connectez-vous pour commenter.

Plus de réponses (1)

Angelo Hafner
Angelo Hafner le 15 Mar 2019
Modifié(e) : Angelo Hafner le 15 Mar 2019
Just enter the u,v,w components in the function log_cv... The function returns the log components of the vector [u,v,w]...
function [log_ax,log_ay,log_az] = log_cv(u,v,w)
r = sqrt(u.^2 + v.^2 + w.^2);
rho = sqrt(u.^2 + v.^2);
t = atan2(rho,w);
f = atan2(v,u);
log_ax = log10(r) .* sin(t) .* cos(f);
log_ay = log10(r) .* sin(t) .* sin(f);
log_az = log10(r) .* cos(t);
end

1 commentaire

Hey Angelo,
This was answered two years ago, and I created a function to solve it, see above.
Your function would not work properly if r will be smaller than 1 (I mean it will work, but will mess up the quiver).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by