Splitting a Colorbar into 12 parts according to months from data

조회 수: 15 (최근 30일)
Sebastian Sunny
Sebastian Sunny 2021년 12월 1일
댓글: DGM 2021년 12월 2일
Hi,
Im trying to use the month function and a for-loop to find and plot specific data according to different months, for example january would be red and february would be blue. I have been trying to have 12 different colors for each month corresponding to each data set.
Thank you

채택된 답변

DGM
DGM 2021년 12월 1일
If you're doing a line plot, it may make more sense to just use a legend.
% the selected colormap
cmap = jet(12);
% dummy data
x = 1:30;
y = rand(12,30)+(1:12).';
% plot setup
hp = plot(x,y);
set(gca,'colororder',cmap)
monthnames = {'Jan','Feb','March','April','May','June','July','August','Sept','Oct','Nov','Dec'};
legend(hp,monthnames);
If you really want to use a discrete colorbar that way instead, you can.
clf;
% the selected colormap
cmap = jet(12);
% dummy data
x = 1:30;
y = rand(12,30)+(1:12).';
% plot setup
plot(x,y)
set(gca,'colororder',cmap)
% wrangle the discrete colorbar tick alignment
colormap(cmap)
cb = colorbar;
cb.Ticks = 1/24:1/12:1;
monthnames = {'Jan','Feb','March','April','May','June','July','August','Sept','Oct','Nov','Dec'};
cb.TickLabels = monthnames;
  댓글 수: 2
Sebastian Sunny
Sebastian Sunny 2021년 12월 1일
Thank you for the answer but i have to implement both a for loop and a month function for this problem. Also the data has timestamps already given to it so how would i implent that into the color scheme.
Thank you
DGM
DGM 2021년 12월 2일
편집: DGM 2021년 12월 2일
See this
and the response to your latest question. I'll see if I can't enhance my answer there. Give me a bit and I'll update it.

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by