# How to plot lat-lon-rainfall in MATLAB?

I have average annual rainfall values at few grids, as shown in below example;

Lat: 25.5 26.5 33.5 34.5 35.5 36.5 41.5 42.5

Lon: 89.5 91.5 78.5 79.5 83.5 84.5 75.5 76.5

Rain: 110 120 122 135 114 116 145 120 110 110

I want to plot it like a map, like this example

Each point will have a cell size of 1 degree, and there must be WGS 84 projection. How to plot them in MATLAB ?

I have around 4000 lat-lon values and these are not in sequence. Moreover the available Lat-Lon represent the irregular shape.

• WGS84 is not a projection. It can refer to a geoid, or a datum. However, the answer doesn't really depend on what specific projection you want; the important thing is that you need to project the output, you can figure out the exact projection later :) Commented Aug 20, 2015 at 9:09
• Do you have access to the Mapping Toolbox? If you're not sure, type ver in interactive Matlab.
– gerrit
Commented Aug 20, 2015 at 9:45
• Using Python with basemap makes it easy. FYI... Commented Aug 20, 2015 at 11:46
• Alternative to the Mapping Toolbox you can try the M_Map mapping package.
– BHF
Commented Aug 20, 2015 at 20:43

What kind of plot do you want?

Lat = [25.5 26.5 33.5 34.5 35.5 36.5 41.5 42.5];

Lon = [89.5 91.5 78.5 79.5 83.5 84.5 75.5 76.5];

Rain = [110 120 122 135 114 116 145 120 110 110];

figure();

plot3(Lat,Lon,Rain,'o')

will give you a scatter plot with Lat being the x, Lon being the y, and rain being the z.

Also note that because your vectors are different legnths, you cannot plot them. You must have vectors of the same lenght to plot (each point must correspond to another point on the axis).

Other types of plots will require you to interpolate between the data points.

If you want to plot it like in the comment below: pcolor is your best choice.

Say your Lat/Lon vectors have n points. Then to make an nxn grid, you can use the command

[X,Y] = meshgrid(Lat,Lon);

This will give you the grid you want, as long as your Lat/Lon Vectors are spaced how you want. To get the 'color" of the pcolor, you will need to have a nxn matrix of rain values: you will need to know what those are at each point. If you do not know those, then then you cannot plot it in map view unless you do some form of modeling or interpolation.

If you have an irregular shape, I would not use matlab. I would use Visit or Paraview, and there are many guides online on how to use this.

If you must use matlab, I would create "fake points" to make the grid a regular shape, and give them false rain values (such as NaN). I would then edit the plot in a postprocessor (photoshop) so that Only the irregular shape appears.

The spacing of the grid does not matter as its specified via your vectors.

• Thanks Neo. I want to plot it like a map, please see the link (i.sstatic.net/ygDyf.jpg) . Each point will have a cell size of 1 degree, and there must be WGS 84 projection. Commented Aug 20, 2015 at 5:54
• Thanks again, I have edited the question. Lat-Lon which I have represent an irregular area, which will create problem with meshgrid. Commented Aug 20, 2015 at 6:06
• My answer has been edited. It seems you are using the wrong tool.
– Neo
Commented Aug 20, 2015 at 6:17
• Many Thanks !! I would be doing some trend analysis and other test in MATLAB and will be plotting result. So I have to go with MATLAB. Seems it can be done with "geoloc2grid" in Matlab. Commented Aug 20, 2015 at 6:23
• I do almost all my analysis in matlab, but almost never plot in matlab for publication, for what its worth. You can export data from matlab (by writing a text file or w/e) which might make it easier to visualize. GMT is especially good at making the plots you seek.
– Neo
Commented Aug 20, 2015 at 7:01

There are two stages to what you want here.

## 1. Converting point data to gridded data

First off, since you want to plot regular cells rather than the actual data that you have, you need to covert your point data to cells in some way. The best way to do this may depend on the data itself, but one way would be to interpolate, perhaps using something along the lines of,

SI = scatteredInterpolant(Lon, Lat, Rain, 'natural', 'linear')
% read up on and understand the last two parameters before choosing what to use

%form the grid, with points 1 degree apart.
gx = min(Lon):1:max(Lon);
gy = min(Lat):1:max(Lat);
[ X, Y ] = meshgrid(gx, gy);

%produce an actual grid of rain data
gridded = SI(X,Y);


Alternatively, if your point data has a much higher resolution than the 1 degree grid, and if it is regularly spaced, you could simply take the mean of the points that lie in each cell.

## 2. Plotting that grid

A naive method, which might be helpful as a quick check that what comes before has worked, would be,

figure;
imagesc(flipud(gridded));
colorbar;


However, that will treat lon and lat as x and y coordinates on a rectilinear grid. To plot with a projection (N.B., WGS84 is not a projection. You'll need to figure out exactly what you're after with more clarity), you ideally want the Mapping Toolbox. I'm not an expert on that, but it should make projected plots relatively straightfoward.

If you don't have access to the Mapping Toolbox, then you could spend a lot of time faffing with coordinate transforms to do what you want... but my advice would be to spend the time learning the basics of a GIS package instead (QGIS is free) and use that to do the job.

• I doubt scatteredInterpolant is a good way to interpolate the lat-lon data. It's not the same as xy data Commented Nov 16, 2017 at 2:43
• @Lee that's a fair point. Only use scatteredInterpolant if it's reasonable to treat lon/lat as cartesian coordinates for what you're doing, ie it's at fairly large scale and not near the poles. Commented Nov 16, 2017 at 4:46