![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/271958/image.png)
Plot grided latitude and longitude as pixels on map
18 次查看(过去 30 天)
显示 更早的评论
I want to plot gridded latitude and longitude as pixels on the map, Latitude is 1 x 32 and longitude is 1 x 40 I don't know how to this I tried geoshow but it said latitude and longitude must have the same size.
Error using checklatlon (line 25)
Function GEOSHOW expected its first and second input arguments,
LAT and LON, to match in size.
Error in geovecshow (line 37)
checklatlon(lat, lon, 'GEOSHOW', 'LAT', 'LON', 1, 2);
Error in geoshow (line 278)
h = showFcn(varargin{:});
I attache latitude and longitude. latitude and longitude are taken from the NetCDF file for gridded precipitation data, but I just want to plot its pixels and I don't need precipitation data.
I want to plot them on this map:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/271952/image.jpeg)
Code to generate map:
% Produce the map
borders('Iran Islamic Republic of') % borders is a function from Matlab file exchange by Chad A Greene (https://www.mathworks.com/matlabcentral/fileexchange/50390-borders)
grid on
axis equal % I added this, important to maintain aspect ratio (@AD)
xlabel('Longitude')
ylabel('Latitude')
xtickangle(90)
I want to plot the pixels like this: (ignore the color bar and colors I just want pixels)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/270608/200208%20154012-Figure%201.png)
Thank you
0 个评论
采纳的回答
Giuseppe Inghilterra
2020-2-16
I obtain this result:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/271958/image.png)
by running the following code:
load lati.mat
load loni.mat
[LONG, LAT] = meshgrid(loni,lati);
Z = rand(length(lati),length(loni));
figure
surface(LONG, LAT, Z)
colorbar
xlabel('Longitude')
ylabel('Latitude')
xticks(loni)
yticks(lati)
xtickangle(90)
grid on
ax = gca;
ax.XAxis.FontSize = 7;
ax.YAxis.FontSize = 7;
In my example I generate ramdomly Z matrix, instead you should have a matrix with data in function of latitude and longitude coordinates. From your plot I see that some "pixels" are blank. This means that you can fill Z matrix with NaN values, in this way you obtain easily blank pixels.
2 个评论
Giuseppe Inghilterra
2020-2-16
编辑:Giuseppe Inghilterra
2020-2-16
See the following code:
- I define a circumference in x,y coordinates;
- then I evaluate if LONG,LAT coordinates are inside the defined circumference through "inpolygon" matlab function and I set to zero these entries of Z matrix;
- then I take the current colormap cmap and redefine the first line with [1 1 1]. In this way I assign color white to "zero" value and then I update colormap;
load lati.mat
load loni.mat
t = 0:pi/10:2*pi; %(1)
x = 55 + 5*cos(t);
y = 30 + 5*sin(t);
[LONG, LAT] = meshgrid(loni,lati);
Z = rand(length(lati),length(loni));
Z(inpolygon(LONG,LAT,x,y)) = 0; %(2)
figure
surface(LONG, LAT, Z)
cmap = colormap; %(3)
cmap(1,:) = [1 1 1];
colormap(cmap)
colorbar
xlabel('Longitude')
ylabel('Latitude')
xticks(loni)
yticks(lati)
xtickangle(90)
grid on
ax = gca;
ax.XAxis.FontSize = 7;
ax.YAxis.FontSize = 7;
I obtain the following resul:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/271977/image.png)
Now, instead of circumference, if you have (x,y) coordinates of your curve, you can obtain the same result.
If you assign to NaN values through inpolygon function Z matrix entries, you don't need to define a custom colormap.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Modify Image Colors 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!