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

6 ビュー (過去 30 日間)
ARINDAM DAS
ARINDAM DAS 2021 年 12 月 1 日
コメント済み: ahmad Saad 2024 年 7 月 19 日
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 ?

回答 (1 件)

Arun
Arun 2024 年 2 月 22 日
編集済み: Arun 2024 年 2 月 22 日
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
  1 件のコメント
ahmad Saad
ahmad Saad 2024 年 7 月 19 日
How we can obtain a 2D plot:
lat as y-axis and long as x axis

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWaveform Design and Signal Synthesis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by