How to plot a trend of a variable for over a spatial region ?
Show older comments
I have a 3-D matrix (lat, lon,time) of a particular variable (
). Here 163 is the number of year. So it contains a total of
timeseries for 163 years . I want to calculate the trend for each timeseries and plot those trends in a figure with the given lat-lon with continental boundaries. Can somebody tell me how to do that ?
timeseries for 163 years . I want to calculate the trend for each timeseries and plot those trends in a figure with the given lat-lon with continental boundaries. Can somebody tell me how to do that ? Answers (1)
Hi Arindam,
I understand that you have data for 180 (15x12) locations, about a trend for 163 years in the form of a 3-D matrix and wish to plot the trend at different locations.
Here are some steps that can be followed to achieve the plot:
- Use “squeeze” function to get the trend values for different years over the different latitude and longitude values.
- Use polynomial fitting using “polyfit” function to calculate the value of trend over the years.
- Create a mesh-grid for latitude and longitude.
- Plot the required trend.
Below is a sample code that performs the above discussed step taking random data to generate a similar plot.
% Assuming you have a 3-D matrix named 'data' with dimensions (lat, lon, time)
% Define the dimensions of the variable
lat = 15;
lon = 12;
time = 163;
% Get the size of the matrix
data = rand(lat, lon, time);
[latSize, lonSize, timeSize] = size(data);
% Preallocate arrays to store the trends and time values
trends = zeros(latSize, lonSize);
timeValues = 1:timeSize;
% Calculate the trend for each timeseries
for lat = 1:latSize
for lon = 1:lonSize
% Extract the timeseries at the current lat and lon
timeseries = squeeze(data(lat, lon, :));
% Calculate the trend using polynomial fitting
p = polyfit(timeValues, timeseries, 1);
% Store the trend value
trends(lat, lon) = p(1);
end
end
% Create a meshgrid for lat and lon
[lonGrid, latGrid] = meshgrid(1:lonSize, 1:latSize);
% Plot the trends
surf(lonGrid, latGrid, trends);
xlabel('Longitude');
ylabel('Latitude');
zlabel('Trend');
Please refer the shared documentation link for more information:
- squeeze: https://www.mathworks.com/help/matlab/ref/squeeze.html
- polyfit: https://www.mathworks.com/help/matlab/ref/polyfit.html
- meshgrid: https://www.mathworks.com/help/matlab/ref/meshgrid.html
- surf: https://www.mathworks.com/help/matlab/ref/surf.html
I hope this helps to plot the required data.
HTH
1 Comment
ahmad Saad
on 19 Jul 2024
How we can obtain a 2D plot:
lat as y-axis and long as x axis
Categories
Find more on Polar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!