I would like a colorbar that only shows the colors of numbers that exist in the dataset.
Example:
x = -2*pi:0.01:2*pi;
y = x;
[X Y] = meshgrid(x,y);
Z = sign(round(cos(X).*cos(Y)));
imagesc(x,y,Z);colorbar;
There are only 3 possible numbers in the output matrix. Unfortunately, the colorbar will still show a full gradient.
I would appreciate a method for generating a colorbar with only the levels that are present.

댓글 수: 1

Sean de Wolski
Sean de Wolski 2011년 9월 7일
+1, well worded clear and concise question

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

 채택된 답변

Sean de Wolski
Sean de Wolski 2011년 9월 7일

1 개 추천

X = sign(round((rand(10)-.5)*2));
imagesc(X);
un_X = unique(X);
colormap(cool(length(un_X)));
H = colorbar;
set(H,'ytick',un_X);
Manually set the colormap and the colorbar's ytick to the unique values of X.

댓글 수: 3

Walter Roberson
Walter Roberson 2011년 9월 7일
I read the question as asking that only the colors actually present in the image appear in the colorbar, not that only those ones are labelled.
Sean de Wolski
Sean de Wolski 2011년 9월 7일
I saw that after I posted, see edit.
Jonathan
Jonathan 2011년 9월 7일
Answered both the question I had, and the one I hadn't thought of.
Thank you

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Color and Styling에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by