How to plot a trend of a variable for over a spatial region ?

6 views (last 30 days)
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 ?

Answers (1)

Arun
Arun on 22 Feb 2024
Edited: Arun on 22 Feb 2024
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:
  1. Use “squeeze” function to get the trend values for different years over the different latitude and longitude values.
  2. Use polynomial fitting using “polyfit” function to calculate the value of trend over the years.
  3. Create a mesh-grid for latitude and longitude.
  4. 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:
  1. squeeze: https://www.mathworks.com/help/matlab/ref/squeeze.html
  2. polyfit: https://www.mathworks.com/help/matlab/ref/polyfit.html
  3. meshgrid: https://www.mathworks.com/help/matlab/ref/meshgrid.html
  4. surf: https://www.mathworks.com/help/matlab/ref/surf.html
I hope this helps to plot the required data.
HTH

Categories

Find more on Waveform Design and Signal Synthesis 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!