Azzera filtri
Azzera filtri

To plot one stem with multiple values for data of different months.

3 visualizzazioni (ultimi 30 giorni)
Dear MATLAB Experts,
I have data in .mat files, each corresponding to a particular month with selected_dates and brightness information.
Each month has a different number of output values (1, 2, 3, ..., N). I need to create a stem plot for each month, with the month on the x-axis and brightness on the y-axis.
For example, if D201701 has six values, the plot will show six values in one single stem, and the x-axis will display the month name.
Additionally, I want to annotate each stem value with a marker (e.g., . or *) to indicate the corresponding date. I have attached an image that shows the stem plot for multiple values, as well as the data and code.
Thank you for guidance and help.
  2 Commenti
Cris LaPierre
Cris LaPierre il 22 Mag 2024
Are you trying to duplicate the image you shared? If so, I don't see how this is a stem plot, since stem plots would all originate from a common horizontal line. It looks like a bunch of line plots to me.
Amjad Iqbal
Amjad Iqbal il 22 Mag 2024
Modificato: Amjad Iqbal il 22 Mag 2024
Yes, I actually wanted to emphasis that I want to plot one stem line for one month to show all the values corresponding to brightness. I understand from image its confusing.
@Voss understood the query and resolved. I appreicate both for time and efforts.
I will further norish the results.

Accedi per commentare.

Risposta accettata

Voss
Voss il 22 Mag 2024
Maybe something along these lines:
unzip('Processed_results.zip')
% load the files
F = dir(fullfile('Processed_results','*.mat'));
NF = numel(F);
for ii = 1:NF
S = load(fullfile(F(ii).folder,F(ii).name));
F(ii).brightness = S.all_brightness_values;
F(ii).dates = S.selected_dates;
end
% count the number of dates and brightnesses in each file
N_dates = arrayfun(@(x)numel(x.dates),F);
N_brightness = arrayfun(@(x)numel(x.brightness),F);
assert(isequal(N_dates,N_brightness))
% collect date and brightness values into matrices;
% each column is a file, each row is a brightness/date
NB = max(N_brightness);
dates = NaT(NB,NF);
brightness = NaN(NB,NF);
for ii = 1:NF
dates(1:N_dates(ii),ii) = F(ii).dates;
brightness(1:N_brightness(ii),ii) = F(ii).brightness;
end
% construct datetime dt representing the start of each month
dt = datetime(year(dates(1,:)),month(dates(1,:)),1);
% markers to be used in plots
markers = '.*xo+^v<>sdph';
markers = repmat(markers,1,ceil(NB/numel(markers)));
% plot stems
figure('Position',[50 50 1500 600])
hold on
for ii = 1:NB
stem(dt,brightness(ii,:),'Marker',markers(ii),'MarkerSize',12)
end
xticks(min(dt):calmonths(1):max(dt))
% or plot markers only
figure('Position',[50 50 1500 600])
hold on
for ii = 1:NB
plot(dt,brightness(ii,:),'LineStyle','none','Marker',markers(ii),'MarkerSize',12)
end
xticks(min(dt):calmonths(1):max(dt))
  2 Commenti
Amjad Iqbal
Amjad Iqbal il 22 Mag 2024
@Voss I really appreciate your kind efforts and providing the solution.
This is what I wanted to plot and show the results. Once again Thank you.

Accedi per commentare.

Più risposte (1)

Mathieu NOE
Mathieu NOE il 22 Mag 2024
I modified a bit your code
added one line in the first for loop
number_of_data_thismonth(k) = numel(brightnessValues); % <= added this line first
then modified the plot
% original plot
% for i = 1:numel(allDates)
% % Plotting the stem for each date
% stem(allDates(i), allBrightnessValues(i), 'filled');
% % Extracting time information
% time_info = datestr(allDates(i), 'HH:MM:SS');
% % Adding time information on top of each stem
% text(allDates(i), allBrightnessValues(i), time_info, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
% end
% modified plot
nnn = 1+[0 cumsum(number_of_data_thismonth)]; % cumulative sum of nb of data per month
for i = 1:numel(number_of_data_thismonth)
ind_start = nnn(i);
ind_stop = nnn(i+1) - 1;
ll(i) = ind_stop - ind_start +1; % ll is equal to number_of_data_thismonth, so we have a working code
% Plotting the stem for each date
% x position is "center" data
ind = round(mean(ind_start:ind_stop));
stem(repmat(allDates(ind),ll(i),1), allBrightnessValues(ind_start:ind_stop), 'filled');
% Extracting time information
time_info = datestr(allDates(ind_start:ind_stop), 'HH:MM:SS');
% Adding time information on top of each stem
text(repmat(allDates(ind),ll(i),1), allBrightnessValues(ind_start:ind_stop), time_info, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
end
your original plot looks like
now the modified plot is :
try it ; hope it helps - full code below
clear;clc;close;
% List all .mat files in the directory
matFiles = dir('*.mat');
% Initialize cell arrays to hold aggregated data
allBrightnessValues = [];
allDates = [];
% Loop through each .mat file
for k = 1:length(matFiles)
% Load the current .mat file
data = load(matFiles(k).name);
% Extract the data
brightnessValues = data.all_brightness_values;
dates = data.selected_dates;
% Ensure data consistency
if isrow(brightnessValues)
brightnessValues = brightnessValues';
end
number_of_data_thismonth(k) = numel(brightnessValues); % <= added this line first
% Convert dates to cell array of strings if necessary
if ischar(dates)
dates = cellstr(dates);
elseif isdatetime(dates)
dates = cellstr(datestr(dates, 'dd-mmm-yyyy HH:MM:ss'));
elseif iscell(dates)
dates = cellfun(@char, dates, 'UniformOutput', false);
else
error('Unexpected date format in file: %s', matFiles(k).name);
end
% Append the data to the aggregated vectors
allBrightnessValues = [allBrightnessValues; brightnessValues];
allDates = [allDates; dates];
end
% Convert the date strings to datetime format
allDates = datetime(allDates, 'InputFormat', 'dd-MMM-yyyy HH:mm:ss');
% Display the size of the aggregated vectors
disp(['Total number of brightness values: ', num2str(length(allBrightnessValues))]);
disp(['Total number of dates: ', num2str(length(allDates))]);
% Plotting
figure;
hold on;
% original plot
% for i = 1:numel(allDates)
% % Plotting the stem for each date
% stem(allDates(i), allBrightnessValues(i), 'filled');
% % Extracting time information
% time_info = datestr(allDates(i), 'HH:MM:SS');
% % Adding time information on top of each stem
% text(allDates(i), allBrightnessValues(i), time_info, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
% end
% modified plot
nnn = 1+[0 cumsum(number_of_data_thismonth)]; % cumulative sum of nb of data per month
for i = 1:numel(number_of_data_thismonth)
ind_start = nnn(i);
ind_stop = nnn(i+1) - 1;
ll(i) = ind_stop - ind_start +1; % ll is equal to number_of_data_thismonth, so we have a working code
% Plotting the stem for each date
% x position is "center" data
ind = round(mean(ind_start:ind_stop));
stem(repmat(allDates(ind),ll(i),1), allBrightnessValues(ind_start:ind_stop), 'filled');
% Extracting time information
time_info = datestr(allDates(ind_start:ind_stop), 'HH:MM:SS');
% Adding time information on top of each stem
text(repmat(allDates(ind),ll(i),1), allBrightnessValues(ind_start:ind_stop), time_info, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
end
hold off;
title('Brightness Temperature vs. Date');
xlabel('Date');
ylabel('Brightness Temperature');
datetick('x', 'yyyy-mm-dd', 'keeplimits');
ylim([180 280])
%%
%Plotting
% figure;
% hold on;
% for i = 1:numel(allDates)
% % Plotting the stem for each date
% stem(allDates(i), allBrightnessValues(i), 'filled');
% % Extracting time information
% time_info = datestr(allDates(i), 'HH:MM:SS');
% % Adding time information on top of each stem
% text(allDates(i), allBrightnessValues(i), time_info, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center');
% end
% hold off;
% title('Brightness Temperature vs. Date');
% xlabel('Date'); axis normal;
% ylabel('Brightness Temperature');
% datetick('x', 'yyyy-mm-dd', 'keeplimits');
  2 Commenti
Amjad Iqbal
Amjad Iqbal il 22 Mag 2024
I really appreciate the time and efforts to provide the solution. I have modified the plot and I wanted to plot a neat one. Attached one is the finest result. I am thank full for all the experts to respond promptly.
I always get benifit from this platform to learn new ideas and adapt for several solutions.

Accedi per commentare.

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by