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.
there is not any example about how to use this code. Please add example to this beautiful code in order to batter and more use for community
The code plots a vector for every data point you give it. If you would like to thin your vectors, then either generate an input field that sub-samples your data (e.g. plotting every second point), or that averages your data (e.g. averaging the positions and data for a 4x4 mat of your data, using this derived field as your input). If you wish to plot pressure-latitude plots, then simply use the Cartesian axis option, as described in the documentation.
As well, do you have a suggestion for how to use this code to generate a vertical cross-section (pressure-latitude) of wind direction and magnitude?
This is some really good code! I am just wondering how I might ba able to modify the spacing between the vectors plotted? I am using the very high resolution NARR dataset which I interpolated to a 0.25x0.25 latitude (~30 km), longitude grid. I feel like this could be the issue, though it seems like the vectors are being plotted even more frequently than the grid-spacing, but that could just be my eyes. Regardless, is there a way to specify the vector spacing without changing the resolution of the data?
Thanks very much,
The link for the code is at the top right under "Download Zip". If, however, you are asking for the particular data used to produce these figures, I am unfortunately not at liberty to release it to Matlab Central at this time.
Where is the code of the example figure?
I want to run the example code and learn the ncquiverref function better.
You can change the size of the vector with the 'reftype' argument. See the documentation at the head of the file for more information.
I have a question: how to change the size of arrow?
In order to make the vector plotting efficient, this routine in fact plots all vectors as one continuos line, separated by NaNs between vectors. Therefore changing the width of individual vectors will require a modification to the code, and of course would require input of a humidity field to guide the line width of the vectors. Plotting vectors individually will, of course, slow down the routine as the vector plotting will need to be placed in some kind of for loop, but it can be done. If you would like to proceed, this is the code that needs to be in the for loop, plotting each vector one at a time:
% Center vectors over grid points
[u,v] = pol2cart(th(mask),scalelength);
% Get x coordinates of each vector plotted
lx = [xstart; ...
% Get y coordinates of each vector plotted
ly = [ystart; ...
% Plot the vectors
When you use the "line" function, set a linewidth scaled by your humidity field.
Thanks for the amazing code. I would like to highlight the arrows plotted on top Relative Humidity WRF model output. For these, I would like to change the width of ALL the arrows (change the 'LineWidth'), and being able to play with it in other to highlight the encounter of dry and moist air with the wind vectors.
Any suggestions will be greatly appreciated.
Thank you very much,
Now the code works fine. Thank you so much! Looking forward to the release of the new analysis package:)
Please package up a test case with data, and send it to me so I can take a look. Before doing this, please try:
I am assuming that in your original message you meant:
size(v_ch) <--- note change from latt in your message
Thanks. My matlab is 2010b version. and I still have the same problem with new version.
Dear Ada, AS et al.,
I have uploaded a new version of this file so that it now works with the graphics changes introduced in R2014b. The appearance is slightly different from the attached figure. I hope this helps. This is an interim measure before the aforementioned package is released this year.
Sorry you are having problems using this with R2014b. ncquiverref is part of a Matlab analysis package for Earth System Models that I have written and will be released in 2015.
In the interim, I believe switching the line:
ncquiverref posted here is an early developmental version, and my current version is vastly different from this posting, and takes advantage of many of the graphics changes in R2014b. It includes callbacks for the colorbar and vector key that allows them to be placed in multi-panel figures using an optimized fit routine not available in the vanilla MATLAB release. As a result, the latest version of ncquiverref is not a stand-alone routine, but an integral part of a larger package primarily focused on analyzing sea ice, ocean and atmosphere models, and the Los Alamos Sea Ice Model, in particular. For this reason I have not provided updates to ncquiverref here, but will be releasing the full package later this year.
I tried to use this function to plot vectors along with ref vector as:
It plotted the vectors in the map but did not print reference vector and got error as follows:
??? Error using ==> text
Invalid parameter/value pair arguments
Error in ==> ncquiverref at 397
Can anyone hep me.
Great code and thanks for sharing! but I get a few error messages when running the code to get coloured vectors.
In Matlab version R2014b, colorbars and legends do not contain handles to underlying objects in their Children properties. Which results in error messages for all the colorbar properties you define. At least for me...
Error using patch
While setting the 'Parent' property of Patch:
Patch cannot be a child of ColorBar.
Error in ncquiverref (line 292)
Any suggestions how to fix this problem?
The example I provided in June 17 2014 was simply to give a square matrix of four (u,v) pairs and their geolocation. Note that the second row is lat = [1 1] (degrees North), lon=[0 1] (degrees east), u=[-1 0] (in specified units), v=[-1 1] (in specified units.
Apologies for the delay in my response.
I was trying to plot my current vectors using your code i.e. ncquiverref. I have a query regarding the dataset:
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];
which you have mention on 17june 2014 in your comment.
what does the second row represents(lat = [1 1]; lon=[-1 0]; u=[-1 0]; v=[-1 1]);
Sir, your advice will be very useful for me.
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.
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.
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.
%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']);
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
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.
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.
My u,v vector in quiverm is strange.....
Do I have to translate to another coordinate or do something else?
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.
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
Thanks a lot. It is a great work.
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.
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?
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:
The main reason you may have to make this manual adjustment is if your vector magnitudes are strongly negatively skewed.
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.
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!
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;
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!!
This update includes fixes the colorbar to enable the function to work with graphics changes in R2014b.
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).
Fixed a bug that occurred when calculating the median vector magnitude when most of the field has vectors of zero length.
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.
This function is a complete rewrite of the originally submitted file.
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.