Plotting of 15 years data on same graph.

I have data of 15 years as i have attached here, which has the format as: Date, time, Pressure data, Temperature Data.
I want to plot the temperature dataset year over year, lets say on x-axis january-december is labeled i want to plot each year data (just Temperature) with different colored plots on the same graph.
Thanks in advance

2 commentaires

Raj
Raj le 11 Déc 2019
There is no attachment..
Zunaira Akmal
Zunaira Akmal le 11 Déc 2019
Attached now.

Connectez-vous pour commenter.

 Réponse acceptée

meghannmarie
meghannmarie le 11 Déc 2019

0 votes

Try something like this:
fid = fopen('Mod21_All_Inclusive_Data_Nov2019.txt');
Mod = textscan(fid,'%s %s %n %n');
fclose(fid); % Close the file
Temp = Mod{3};
DMY =cellstr(Mod{1});
HM = cellstr(Mod{2});
DMYHM = datetime(DMY,'InputFormat','MM/dd/yy') + duration(HM,'InputFormat','hh:mm');
Y = year(DMYHM);
yrs = unique(Y);
x_axis = day(DMYHM,'dayofyear') + (hour(DMYHM) + (minute(DMYHM)/60))/24;
fig = figure;
hold on
for t = 1:numel(yrs)
yr = yrs(t);
idx = yr == Y;
plot(x_axis(idx),Temp(idx));
end
XTicks = nan(1,12);
XTickLabels = cell(12,1);
for n = 1:12
dt = datetime(2019,n,01);
XTicks(n) = day(dt,'dayofyear');
XTickLabels(n) = month(dt,'name');
end
ax = gca;
ax.XTick = XTicks;
ax.XTickLabel = XTickLabels;
fclose(fid);

8 commentaires

Zunaira Akmal
Zunaira Akmal le 11 Déc 2019
Thank you for your response but the above code is ploting my data from January-December however there are certain years whose data does not start with january.
meghannmarie
meghannmarie le 11 Déc 2019
It shouldn't matter, but let me look. What year doesnt start with January?
Zunaira Akmal
Zunaira Akmal le 11 Déc 2019
The first year, i.e. 2005.
meghannmarie
meghannmarie le 11 Déc 2019
This code does not start plotting until julian day 240 for that year. I put in a few lines to plot 2005 in a thick red line so you can see its not plotting in first half of year.
fid = fopen('Mod21_All_Inclusive_Data_Nov2019.txt');
Mod = textscan(fid,'%s %s %n %n');
fclose(fid); % Close the file
Temp = Mod{3};
DMY =cellstr(Mod{1});
HM = cellstr(Mod{2});
DMYHM = datetime(DMY,'InputFormat','MM/dd/yy') + duration(HM,'InputFormat','hh:mm');
Y = year(DMYHM);
yrs = unique(Y);
% x_axis = day(DMYHM,'dayofyear')
x_axis = day(DMYHM,'dayofyear') + (hour(DMYHM) + (minute(DMYHM)/60))/24;
fig = figure;
hold on
for t = 1:numel(yrs)
yr = yrs(t);
idx = yr == Y;
plot(x_axis(idx),Temp(idx));
end
%this plots thick red line for 2005
yr = 2005;
idx = yr == Y;
plot(x_axis(idx),Temp(idx));
if month(min(DMYHM(idx))) > 1
plot(x_axis(idx),Temp(idx),'r','LineWidth',5);
end
XTicks = nan(1,12);
XTickLabels = cell(12,1);
for n = 1:12
dt = datetime(2019,n,01);
XTicks(n) = day(dt,'dayofyear');
XTickLabels(n) = month(dt,'name');
end
ax = gca;
ax.XTick = XTicks;
ax.XTickLabel = XTickLabels;
Zunaira Akmal
Zunaira Akmal le 12 Déc 2019
Problem is, if you see data then you will come to know that there are certain years in which you will see a gap in months, would you please tell me how your code is handling such data? Yes, for 2015 it is fine but i can not make it to complete dataset.
Thank you
meghannmarie
meghannmarie le 12 Déc 2019
it is a line plot so as far as the gaps in months go, thats where you see those long lines connecting the beginning to end of data gaps. If you do not like those long lines connecting the data gaps, you can identify the data gaps and plot those lines separately. Also note, this is plotting data according to day of the year. So where the months are labeled according to a non-leap year.
dpb
dpb le 12 Déc 2019
"If you do not like those long lines connecting the data gaps, you can identify the data gaps and plot those lines separately."
Or fill missing data w/ NaN and will be silently ignored by plot. Still takes finding the breakpoints but would keep one line handle per year for things like legend, linestyle etc., ...
Zunaira Akmal
Zunaira Akmal le 13 Déc 2019
Thank you very much meghannmarie for your answer, it has solved my problem.
Kind regards

Connectez-vous pour commenter.

Plus de réponses (1)

Hiro Yoshino
Hiro Yoshino le 11 Déc 2019

0 votes

Convert the date information to datetime type and extract just "months" and "days" using the functions, "month" and "day".
This way, you can remove "year" information from your dataset and thus you can line them up in the same x-axis.

6 commentaires

Zunaira Akmal
Zunaira Akmal le 11 Déc 2019
I have converted it using datetime but the problem is i am unable to get the corresponding temperature data from file, can you send me a code please?
Thanks
I cannot tell what you mean since you haven't uploaded your data though, I guess you should have all the data as a table variable and
monthdata = month(your_table.Date)
daydata = day(your_table.Date)
new_timedata = datetime(monthdata,daydata) % Not sure if this line works
your_table.Date = new_timedata; % Update your table
This is a rough idea but I belive this is good enough for you to move forward. :-)
Zunaira Akmal
Zunaira Akmal le 11 Déc 2019
Zunaira Akmal
Zunaira Akmal le 11 Déc 2019
According to the code you sent, monthdata and daydata is extracting month and day from the dates not the corresponding data. i want to plot corresponding data year by year, i hope i am clear now. However, i have attached the dataset file now.
dpb
dpb le 11 Déc 2019
Use the new_timedata vector as x, the array of data by column as y and plot will automagically do the rest...
Have you tried anything? This is a cooperative venture, show us what you have done.
Zunaira Akmal
Zunaira Akmal le 11 Déc 2019
Modifié(e) : per isakson le 13 Déc 2019
If i do what you said then it will give me a plot of my complete dataset which i do not want, i want to plot data year by year, lets say first year temperature data with red color, 2nd year tempearture data with blue color and so on, i hope you are getting my point.
Yes, i have tried to plot using the code below, but now the problem is how can i extract data correspeonding to the respective data of temperature.
fid = fopen('Mod12_All_Inclusive_Data_Nov2019.txt');
Mod = textscan(fid,'%s %s %n %n');
fclose(fid); % Close the file
Temp = Mod{3};
Year=Mod{1};
Year2=year(Year);
length(Year2);
[a,b] = histc(Year2,unique(Year2));

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by