필터 지우기
필터 지우기

How to plot multiple year data over common months?

조회 수: 6 (최근 30일)
JMSE
JMSE 2022년 1월 5일
댓글: JMSE 2022년 1월 6일
Dear all,
how to plot data from multiple years over one common x-axis? I have some test data measured in january over 4 years, and I want to color the data corresponding the different years. This testdata (datetime data & measurement data) containts data for January only, however future measurements may contain other months, too. So I would like to have x.axis ticks 1-12 (months) and user a scatterplot(?) to mark the points corresponding to n years (colormarker). Please find the test data attached and many thanks for help in advance!
dt=readtable("Test.xlsx");
plot (dt.Date, dt.NO2,"r.")
  댓글 수: 1
Star Strider
Star Strider 2022년 1월 5일
Please provide more data. There is only one month (January) for all four years in that file excerpt.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2022년 1월 5일
With different markers as well as different colors (different colors alone is easier.)
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/853865/Test.xlsx';
dt = readtable(filename);
[y, m, d] = ymd(dt.Date);
baseyear = min(y);
lastyear = max(y);
cmap = colormap(jet(lastyear-baseyear+1));
G = y - baseyear + 1;
x = datetime(baseyear, m, 1, 'Format', 'MM');
pointsize = 30;
markers = {'.', '+', '*', '<', 'o', 's'}.';
assert(length(markers) >= max(G), 'Need more markers');
markermap = markers(G);
hold on
splitapply(@(M,NO2,c,mark) scatter(M, NO2, pointsize, c(1,:), mark{1}), x, dt.NO2, cmap(G,:), markermap, G);
%scatter(x, dt.NO2, pointsize, cmap(y-baseyear+1,:));
hold off
xlim auto; ylim auto;
ax = gca; ax.XRuler.TickLabelFormat = 'MMM';
xticks(datetime(baseyear,1:12,1))
  댓글 수: 8
JMSE
JMSE 2022년 1월 6일
Actually, would it not be simpler to split the datetime into 2 arrays (1: day and months; 2: year) and to scatter it with scatter(x = day and month, y = measurements, sz = pointsize, c = year)? However, I o not know yet how to split the datetime array into two seperate arrays, I will try to figure that out. Any hint is very welcome of course.
JMSE
JMSE 2022년 1월 6일
This does what I want:
dt=readtable("Test.xlsx");
d = day(dt.Date,'dayofyear');
m = month(dt.Date);
y = year(dt.Date);
baseyear = min(y);
lastyear = max(y);
cmap = colormap(jet(lastyear-baseyear+1));
G = y - baseyear + 1;
scatter(d,dt.NO2,[],y)
xlim([-.5 365.5])
xticks([0 31 59 90 120 151 181 212 243 273 304 334])
xticklabels({'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'})
ylim([0 5])
hold on
nG = max(G);
L = gobjects(1, nG);
for K = 1 : nG;
L(K) = plot(nan, nan, 'color', cmap(K,:));
end
hold off
title ("")
xlabel ("Months")
ylabel ("uM nitrite")
legend(L, string(baseyear:lastyear))
grid on
box off
Thank you very much for your ideas and support @Walter Roberson!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by