contourm plotting

1 次查看(过去 30 天)
ash salut
ash salut 2012-3-17
回答: Sanchari 2024-7-25
I have a text file containing Lats and Lons. I want to plot a geomap with 0.1 deg grid. Then the map use a color bar, showing the number of occurrences of the Lats and Longs.

回答(1 个)

Sanchari
Sanchari 2024-7-25
Hello Ash,
To plot a geomap with a 0.1-degree grid and use a color bar to show the number of occurrences of the latitude and longitude points from a text file, please follow these given (high-level) steps in MATLAB:
  1. Read the data from the text file.
  2. Create a grid with 0.1-degree resolution.
  3. Count the occurrences of latitude and longitude points in each grid cell.
  4. Plot the data on a geomap with a color bar.
Here's a complete example:
% Read the data from the text file
data = readmatrix('latlon.txt');
lats = data(:, 1);
lons = data(:, 2);
% Define the grid resolution
gridResolution = 0.1;
% Create the grid
latEdges = min(lats):gridResolution:max(lats);
lonEdges = min(lons):gridResolution:max(lons);
% Count occurrences in each grid cell
occurrences = histcounts2(lats, lons, latEdges, lonEdges);
% Create a figure
figure;
% Create a map with the specified grid resolution
latCenters = latEdges(1:end-1) + gridResolution/2;
lonCenters = lonEdges(1:end-1) + gridResolution/2;
% Plot the data using pcolor
pcolor(lonCenters, latCenters, occurrences);
shading flat;
colorbar;
xlabel('Longitude');
ylabel('Latitude');
title('Number of Occurrences of Lats and Lons');
% Optionally, use geoscatter for a more geographic visualization
% figure;
% geoscatter(lats, lons, [], occurrences(:), 'filled');
% colorbar;
% title('Number of Occurrences of Lats and Lons');
This code will create a geomap with a color bar indicating the number of occurrences of each latitude and longitude point within the specified grid cells.
Notes:
  • Ensure that the text file "latlon.txt" is in the correct format and located in the current working directory or provide the full path to the file.
  • Adjust the "gridResolution" if there is a need of different resolution.
  • The "pcolor" function is used for plotting the grid data. If one prefers a geographic scatter plot, one can use "geoscatter" as shown in the commented section.
One can find more examples in the MathWorks Documentation for "contourm": Thematic Maps — Examples (mathworks.com)
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by