Code covered by the BSD License  

Highlights from


4.8 | 11 ratings Rate this file 63 Downloads (last 30 days) File Size: 5.74 KB File ID: #17582
image thumbnail




18 Nov 2007 (Updated )

Quiver (vector) with a reference vector or color code on a map or Cartesian axes.

| Watch this File

File Information

This function is a substitute for the standard versions of quiver and quiverm available using a vanilla release of matlab. This version assumes a 2D vector field is being plotted using a gridded flow field from a numerical model. It is primarily intended for Earth System Model analysis.

The function enables the scaling of vectors according to a reference vector plotted in the lower right hand corner of the plot axes. The function works for both map and Cartesian axes and allows the color of vectors to be changed.

If a reference value is not provided, the reference value is calculated by rounding the median or maximum magnitude of the quiver vectors. Scaling of vectors still occurs even if the reference vector plotting is switched off. This enables different subplots to share identical scaling so that the relative magnitude of vectors can be compared between subplots (provided they share the same grid).

The function also includes the ability to plot color vectors, all of equal length but color coded according to their magnitude. In this case, a colorbar is provided, complete with units, and no scaling vector is plotted.

Vectors are centered on the grid points they represent, rather than the tail of the arrow being positioned on the grid point as is the case for the vanila Matlab quiver and quiverm versions.


This file inspired 2 D Vector Field Visualization, Quivermc, and Antarctic Mapping Tools.

Required Products Mapping Toolbox
MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (19)
18 Jun 2014 K


Dang, you are right of course. In my excitement to see something that would plot geographic-coordinate vectors I failed to internalize the very clear statement that the code assumes a gridded field. Guess I'll keep looking and/or work on writing some code to do what I need.

Thanks for your prompt response. It's much appreciated.

17 Jun 2014 Andrew Roberts

Hello "K",

The documentation clearly states that "This version assumes a 2D vector field being plotted using a gridded flow field from numerical models with a regular geometry".

If you set:

lat=[41.1700 -73.1300; 1.0000 1.0000];
lon=[35.2200 -75.6200; 0 1.0000];
u=[30.3800 -84.3500; -1.0000 0];
v=[25.9000 -97.4200; -1.0000 1.0000];

You will find that you get output, and the function works as designed and documented.

If your inputs are not on a grid, then this is not the function for you. Alternatively, if your inputs are on a 2D grid, but are organized into a vector, then you will need to reorganize your data back onto the 2D grid.

- Andrew

17 Jun 2014 K

This code looks like it'll do what I want it to do, which is plot vectors (or straight-up direction arrows) in geographic coordinates. Thanks! Unfortunately I'm getting an error at line 151:

[u,v] = pol2cart(th+deg2rad(thproj),z);

The problem is that in my application th is a vector (n rows, one column) but thproj is an array (n rows, n columns). Perhaps I'm just misinterpreting the input variables?

%Clear and close all files, windows, etc.
close all;

%Open Excel Data File
filename=input('enter filename: ','s');
lat=data(:,1); %create latitude vector
lon=data(:,2); %create longitude vector
u=data(:,3); %create u-component vector;
v=data(:,4); %create v-component vector;

%Create a map
ax = usamap('conus');
set(ax, 'Visible', 'off')
latlim = getm(ax, 'MapLatLimit');
lonlim = getm(ax, 'MapLonLimit');
states = shaperead('usastatehi',...
'UseGeoCoords', true, 'BoundingBox', [lonlim', latlim']);
geoshow(ax, states,'FaceColor','white')
grid off;

%Plot arrows

Here's my test data file (lat, lon, u, v):
41.1700 -73.1300 1.0000 1.0000
35.2200 -75.6200 0 1.0000
30.3800 -84.3500 -1.0000 0
25.9000 -97.4200 -1.0000 1.0000

21 May 2014 Chad Greene

This function is an incredible improvement over quiverm for two key reasons:

1. quiverm confoundingly thinks insists that u is a meridional component and v is a zonal component, but ncquiverref gets it right.

2. quiverm scales relative to lats and lons, so if you're plotting an absolute field such as surface wind velocity, vectors shrink near the poles using quiverm whereas ncquiverref gets it right.

The only two changes I'd make are 1. switch to a more intuitive name for the function (quite trivial and users can easily do this on their own) and 2. add an option to easily switch between centering arrows over their reference points or starting them at the reference points, because which the user prefers may depend on the data being plotted.

Overall a fantastic, adaptable, and robust script. Thanks for sharing, Andrew.

27 Jan 2014 Andrew Roberts

Hi Nelson,

It's almost impossible to understand what you are doing without an example. Most problems with ncquiverref result from the data not being prepared as specified in the documentation, so check this carefully. If you are unable to resolve the problem, please send me your script and some sample data so that I can check on what you are doing, and then I'll let you know if I can help.

Andrew Roberts

24 Jan 2014 Nelson

My u,v vector in quiverm is strange.....
Do I have to translate to another coordinate or do something else?

31 Oct 2013 Eowyn Baughman

An enormous improvement on quiver when working with geophysical model data. If you want to chance the color of the reference scale vector, change line 440 to line(lx,ly,lz,'Color','k');

This way you can overplot white vectors on a colored figure but have a visible reference vector.

20 May 2013 Boris Mifka

Dear Matlab users. Does anyone use ncquiverref function? I have a problem because it has error due to 208 line , index exceeds matrix dimensions. I didn t manage to find out what is the problem , and i need this referent arrow on my plot

08 Dec 2012 Prashanth hanmaiahgari

Thanks a lot. It is a great work.

21 Mar 2012 Diana

I am having trouble running this code.. just to be double sure, what should I specify in contours?

I'm getting this error message:
??? Index exceeds matrix dimensions.

Error in ==> ncquiverref at 208

I would be very thankful if any suggestions are made.

25 May 2011 Cheryl Harrison

Is there any way to get this to plot vectors over a pcolor plot? I can get the vectors on just fine, but as soon as I plot the reference vector it whites everything out. Modification ideas?

25 Jan 2011 Josh Jones  
21 Jan 2011 Thibaut Leroy  
01 Sep 2010 Andrew Roberts

Hi Hamed,

If you have tested using "max" instead of "median" for reftype, or your color vectors are too long, it is easy to adjust the variable "scalelength" to a fraction of its current value in the code. Change line 186 to:
where 0<X<1.
The main reason you may have to make this manual adjustment is if your vector magnitudes are strongly negatively skewed.


01 Sep 2010 Hamed Babazadeh Babazadeh

Great code, just a question,

how to scale vector field as it is in quiver command, the default size of vectors is too much for my application.


20 Feb 2010 Chong Zhou

Genius work! It gives amazing visualisation of large amount of vectors, which is common in the data post-processing in article image velocimetry (PIV). The author also improved the code to better adapt to the application in PIV, where 'axis ij' is a must to plot pixel coordinate.

Again, great work and I strongly suggest people using PIV try this code!

Many thanks!

02 Oct 2009 Jason Sippel

Great code! After chatting with the author, I have found that the below addendum is useful for plotting a small subdomian of model output. This would be useful if, for example, you are tracking a tropical cyclone in a Lagrangian system with ilat,flat,ilon, and flon sampled from your model grid based on the number of grid points from the storm center.

If you don't do something similar to this, the physical size of the map frame on the screen may not match from one figure to the next.

Here's the code:


cutfactor = 1.3;
set(gca,'Xlim',[xcenter-cutfactor*xoffset xcenter+cutfactor*xoffset]);
set(gca,'Ylim',[ycenter-cutfactor*yoffset ycenter+cutfactor*yoffset]);

25 Sep 2009 Jason Sippel  
02 Jul 2009 Ibrahim M

This is genius!

For it to work in multiple colors, you make sure your x and y are produced with/as meshgrid. Also specify your colors according to your choice of colorbar. Eg if your caxis ([15 30]) for SST, then you should have the function as:

ncquiverref(x,y,xx,yy, 'DegC','mean',0,'col',[15 20 25 30]);

Nice work Andrew!

Thank you very much for sharing!!


21 Jan 2009

This is a complete rewrite of the originally posted function. Vectors are generated within this function rather than using quiver and quiverm, are now centered on the grid points, and have the ability to be color coded according to their magnitude.

22 Jan 2009

This function is a complete rewrite of the originally submitted file.

19 Feb 2010

This file has been amended so that meshgrid does not need to be run prior to using the function. Also, a small bug has been fixed to allow the use of axis ij to invert the y axis for non-color plots.

20 Jan 2011

Fixed a bug that occurred when calculating the median vector magnitude when most of the field has vectors of zero length.

14 Feb 2011

Removed use of the Latex interpreter to avoid a bug in MATLAB R2010b. The Latex text interpreter may cause Matlab to crash when using the 'nodisplay' option (this bug has been reported to Mathworks).

Contact us