Plot a mean of ten lines of different resolution
1 view (last 30 days)
Show older comments
Hello all,
I would like to plot the mean of ten lines. Each line plots the data from a different longitude and latitude against a timeslice. The data (mean annual temperature over the past 120,000 years) only covers temperatures on land, and as such, some timeslices have no corresponding temperature data for the years in which they were submerged.
The data dimensions are as follows:
Longitude: 720x1
Latitude: 300x1
Month: 12x1
Year: 72x1
Monthly temperature: 720x300x12x72
Can anyone advise on how to plot the mean of these ten timeslices? That is to say, the mean temperature over the past 120,000 years across the ten coordinate points plotted. I am very new to MATLAB and programming generally so help would be greatly appreciated.
my_year = -10000;
my_month = 6;
my_longitude = 11.2980;
my_latitude = 62.4342;
[~,lonID] = min(abs(longitude - my_longitude));
[~,latID] = min(abs(latitude - my_latitude));
[~,yearID] = min(abs(years - my_year));
mean_annual_temperature = mean(temperature,3);
plot(years, squeeze(mean_annual_temperature(lonID, latID, :)), 'Marker', 'none', 'Color', '#fff100');
title(['Mean annual temperature time series, 60N to 65N']);
xlabel('Year');
ylabel('Temperature °C');
4 Comments
MarKf
on 9 Jul 2023
Edited: MarKf
on 9 Jul 2023
Still unclear what you need the mean of and what do you need to plot from your description. What are the 10 lines in the 120,000 values?
I assume from the plot and the code that you want the annual average, that is averaging all the months in the years so to have a single mean value per year, to be clear, 72 yearly scores for each of the 720 longs times 300 lats. That would be 216000*72 average values, but I'm guessing some are missing so that's how you get 120000? (also misleading to call them 120,000 years, having 72 years of recording is impressive but hundreds of thousands? a strage way to put it).
Again from the plot, the missing values seem to be NaNs already, that'd be conveninent but do check that. In that case mean_annual = mean(temp,3,"omitnan") will ignore the missing values, otherwise just temp(indecesofmissing) = nan first, so that'd take care of that.
You're then finding an index of lats and longs (and also a yearID, but that's ignored) so I'm guessing that's how you choose those 10 annual vectors, there is no way for us to know without having the full code, variables and data. When you select those data, subsetemp = squeeze(mean_annual_temperature(lonID, latID, :) would work if the indeces lonID, latID were 2 ranges, so I assume the variables longitude and latitude are. And there's the rub for having "different resolutions of longitude and latitude against a timeslice", but again not sure what you need there.
Accepted Answer
Star Strider
on 9 Jul 2023
It would help to have the actual data.
The missing data must be NaN values, since if they were simply blank, they would throw an error using the posted code. The result of ‘squeeze(mean_annual_temperature(lonID, latID, :))’ is therefore a 2D matrix, although I cannot determine its size relative to ‘years’ since plot corrects for that.
Guessing at the data, perhaps something like this —
mean_annual_temperature = randn(10, 72); % Create Data
mean_annual_temperature(5,[1:5 30:35 67:72]) = NaN; % Insert Some 'NaN' Values In One Row
years = 1:72; % Create Data
A = [years; mean_annual_temperature] % Concatenate MAtrices
NaNidx = any(isnan(A),1) % Determine 'NaN' Columns
A = rmmissing(A, 2) % Remove Missing Values Columns
Ayears = A(1,:) % Recover Vector
Amean = mean(A(2:end,:)) % Mean Of Remaining Columns
B = NaN(size(years)); % Create 'NaN' Vector
B(~NaNidx) = Amean % Assign Mean Values To 'Non-NaN' Index Positions
figure
hp1 = plot(years, mean_annual_temperature, 'DisplayName','Temperature MAtrix');
hold on
hp2 = plot(years, B, '-k', 'LineWidth',2, 'DisplayName','Mean Temperatures');
hold off
title(['Mean annual temperature time series, 60N to 65N']);
xlabel('Year');
ylabel('Temperature °C');
legend([hp1(1) hp2], 'Location','best')
This plots only the data for the years in which no data are missing. I believe this is preferable to using the mean 'omitmissing' flag, since this approach only uses data from years in which all data are present.
.
8 Comments
Star Strider
on 10 Jul 2023
My pleasure!
The colours are as you defined them.
To see that the idea actually works experiment with:
mycolors = colormap(turbo(10));
If my Answer helped you solve your problem, please Accept it!
.
More Answers (1)
MarKf
on 9 Jul 2023
I see, this is overly complicated. Not as in complex or difficult, as in made needlessly overly complicated.
Let's just say you have 72 timepoints (or slices). You average the months as above (which is a point that is now unclear to me since we are talking about geological eras but whatever) and get 72 timepoints. I can see you already can plot those timepoints with the right scale with plot(years, mean_annual) so no probl. I'd say you'd be more interested in having the mean data points even if some locations or times are missing, so likely just 'omitnan' as I mention previously for now, rather than rmmissing as in the answer above.
In the map image you then have 10 random coordinates for each of 6 latitude ranges of 5 degrees, from south to north EU. So in this case you have 60 locations. Each location has a lat and a longit obvs. You can have a matrix of 60*2 of lats and a longits keeping track of the location and then a 60*72 mat of locations*timepoints vectors (timeseries). I'm unsure how you get the raw data then, but I assume you can easily create these 2 variable from what you got so far.
Then you just select the timeseries you want to average and/or plot based on the geograhical range of your choice. Maybe this code snippet can give you an idea.
lats = 35:5:65; longs = [-10 50]; lpoints = 10;
yearsn = 72; years = [-120000:2000:-22000 -19000:1000:2000];
loc = [];
for il = 1:numel(lats)-1
random_lats = (lats(il+1)-lats(il)).*rand(lpoints,1) + lats(il); %just a quickly randomly generated example
random_longs = (longs(2)-longs(1)).*rand(lpoints,1) + longs(1); %likely not to have the 200km apart requirement
loc = [loc;random_lats,random_longs]; %in this case they would be already ordered in groups of 5°
end
mean_annual_temp = randn(size(loc,1), yearsn); % random mean annual temperature data
mean_annual_temp(randi([1 numel(mean_annual_temp)],1,100)) = NaN; %add missing
%select locs based on lat (even if already ordered in groups of 5°, just in case)
range_lat = [43 48]; % you could also do this in a loop as above with like for il = 1:numel(lats)-1, [lats(il) lats(il+1)]
loc_idx = loc(:,1)>=range_lat(1) & loc(:,1)<range_lat(2);
subsetmat = mean_annual_temp(loc_idx,:); % you could selct a time range with (:,time_idx); as well
subsetmean = mean(subsetmat,'omitnan');
%then same as above
figure
plot(years, subsetmat); %already plots all with diff colors
hold on
plot(years, subsetmean, '-k', 'LineWidth', 3);
hold off
title(sprintf('Mean annual temperature time series, %dN to %dN',range_lat));
xlabel('Year');
ylabel('Temperature °C');
0 Comments
See Also
Categories
Find more on Scatter 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!