I am trying to plot a heatmap as the following:
data = [2 1 0 0 0 0 0 0 0 0;1 2 15 0 0 0 0 0 0 0];
colormap('hot');
imagesc(data);
colorbar;
But I want a colorbar with only the values in my data. I don't really need the "transitioning" ones. Is there a way to do so?
Thanks!

 채택된 답변

Kelly Kearney
Kelly Kearney 2016년 1월 16일

0 개 추천

The unique values in you original question are [0 1 2 15]. Do you want just those to appear in the colorbar, or all possible integers between 0 and 15?
For the former:
data = [2 1 0 0 0 0 0 0 0 0;1 2 15 0 0 0 0 0 0 0];
[unq, ~, iunq] = unique(data);
iunq = reshape(iunq, size(data));
ncol = length(unq);
cmap = hot(ncol*2);
cmap = cmap(1:2:end,:);
imagesc(iunq);
colormap(cmap);
cb = colorbar;
set(gca, 'clim', [0.5 ncol+0.5]);
set(cb, 'ticks', 1:ncol, 'ticklabels', cellstr(num2str(unq)));
Walter's code covers the latter case. As a side note, I'd usually just suggest hot(4) to get the 4-color hot colormap, but interpolating hot to only 4 colors results in two nearly-identical shades of yellow; hence the doubling of colors and then using every other in my cmap generation above.

댓글 수: 5

Walter Roberson
Walter Roberson 2016년 1월 16일
My code covers labeling only those values, but would generate the intermediate colors as well.
Speaking of intermediate colors, I would suggest that ncol be computed from max() and min() of the data, the colormap generated, and then indexed at the appropriate indices for the unique values. In that way the the colors would reflect relative values, whereas with your scheme, the colors only represent ordering of values.
Arthur Brigatto
Arthur Brigatto 2016년 1월 18일
thanks for replying!
I am getting the following error: "The name 'ticks' is not an accessible property for an instance of class 'colorbar'."
Perhaps MATLAB version?
Arthur Brigatto
Arthur Brigatto 2016년 1월 18일
Sorry, get it now! replaced ticks by ytick and tyckslabel by yticklabel and now it works. So far I have what I need! Thank you very much.
Kelly Kearney
Kelly Kearney 2016년 1월 19일
The 'ticks', 'ticklabel' combo is necessary for HG2 (R2014b+). For earlier versions, substitute '[x/y]tick', '[x/y]ticklabel' as appropriate depending on colorbar orientation.
AASHNA SUNEJA
AASHNA SUNEJA 2020년 10월 29일
Hi
I want my data [4.64 5.99 4.97 5.71 5.34 8.1 8.01 6.12 5.58 8.11 7.94 7.16 7.25 7.5 7.59] to be plotted in 15 equal regions of a circle. Each value to be represented by different color. How I can I do this? The code given above plots the values in a stairght line. For reference I am attaching a plot. A similar profile I want from my experiemntal values. Can anyone help me with this.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 1월 11일

0 개 추천

data_range = ceil(max(data(:))) - floor(min(data(:))) + 1;
imagesc(data);
colormap(hot(data_range));
colorbar();

댓글 수: 2

Arthur Brigatto
Arthur Brigatto 2016년 1월 15일
Perhaps I did not make myself clear. Plotting like this the colorbar displays values like [2 1.8 1.6 1.4 1.2 1 0.8...0]. I want only 2, 1 and 0.
Walter Roberson
Walter Roberson 2016년 1월 15일
For MATLAB R2014a and earlier, colorbar() creates a new axes, which you can record the handle of and set() the YTick parameter of.
You would probably want to be setting the ticks at unique(data(:))

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

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

질문:

2016년 1월 11일

댓글:

2020년 10월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by