colorbar to vary brightness level given a certain color hue

8 views (last 30 days)
I use the following code to produce these types of 2D histograms, one for each of several conditions:
matrix = [ratingsx(:) ratingsy(:)];
values = hist3(matrix,[7 7]);
values = values';
imagesc(values)
colorbar('Ticks',[0 max(max(values))], 'TickLabels',{'0',[num2str(max(max(values)))]})
colormap gray;
Right now the colorbars are all greyscale across all conditions, but in fact each condition has had, in a previous graph, a different colour associated with it (e.g. "Classical"=red, "Random"=yellow, etc), and I would like the colourbars to reflect that respective hue, i.e. go from dark red to light red for "Classical" etc.
How can I achieve that? Many thanks!

Answers (2)

Guillaume
Guillaume on 5 Jul 2016
As you said, you want to replace the hue. So, grab the grey colour map, convert to HSV (Hue, Saturation, Value), replace the grey hue by whichever hue you want, and convert it back to RGB. Because, the saturation is also 0 in the gray colour map, you also need to increase it.
originalrgb = [0.5 0.3 0.7]; %replace by whatever rgb colour you want
originalhsv = rgb2hsv(originalrgb); %get the HSV values of your original colour. We really only care about the hue
maphsv = rgb2hsv(gray); %without any argument gray returns 64 values; convert to hsv
maphsv(:, 1) = originalhsv(1); %replace gray hue by original hue
maphsv(:, 2) = originalhsv(2); %replace saturation. Anything but 0 will work
newmap = hsv2rgb(maphsv);
colormap(newmap);
  1 Comment
z8080
z8080 on 5 Jul 2016
Thanks! I must have used your code correctly because I am still getting a grayscale colorbar. Here's my full code:
rgb_classical = [0 131 76]/255;
rgb_jazz = [197 194 69]/255;
rgb_random = [61 92 160]/255;
rgb_avantgarde = [0 194 188]/255;
for i_genre=1:N_genre
x = ratings(:, i_genre, :, :, 1);
y = ratings(:, i_genre, :, :, 2);
x = reshape(x,1,[]);
y = reshape(y,1,[]);
subplot(2,2,i_genre);
matrix = [x(:) y(:)];
values = hist3(matrix,[rating_max rating_max]);
imagesc(values)
colorbar('Ticks',[0 max(max(values))], 'TickLabels',{'0',[num2str(max(max(values)))]})
axis tight
axis equal
axis xy
xlabel('Pleasantness', 'FontSize', fontSize_labels)
set(gca, 'XTick', [1 rating_max], 'FontSize', fontSize_values);
set(gca, 'XTickLabel', {'Lo' 'Hi'})
ylabel('Roughness', 'FontSize', fontSize_labels)
set(gca, 'YTick', [1 rating_max], 'FontSize', fontSize_values);
set(gca, 'YTickLabel', {'Lo' 'Hi'}) % NO NEED TO FLIP THE RGH AXIS AS THE VALUES THEMSELVES HAVE BEEN FLIPPED IN ADVANCE (8-..)
switch i_genre
case 1 % just trying out this one for now
genre='Avantgarde';
originalhsv = rgb2hsv(rgb_avantgarde); %get the HSV values of your original colour. We really only care about the hue
maphsv = rgb2hsv(gray); %without any argument gray returns 64 values; convert to hsv
maphsv(:, 1) = originalhsv(1); %replace gray hue by original hue
maphsv(:, 2) = originalhsv(2); %replace saturation. Anything but 0 will work
newmap = hsv2rgb(maphsv);
colormap(newmap);
case 2
genre='Classical';
colormap gray
case 3
genre='Jazz';
colormap gray
case 4
genre='Random';
colormap gray
end
title(genre, 'FontSize',fontSize_titles)
end

Sign in to comment.


Image Analyst
Image Analyst on 5 Jul 2016
Edited: Image Analyst on 5 Jul 2016
Take your original colormap and see if there are any constant hues in there. There may not be any, so you'd have to construct such a colormap. I suggest you therefore create the colormap in hsv color space, then use hsv2rgb() to map it back to RGB colors, then use colormap() to apply that RGB colormap to your axes. Construct the HSV colormap with the first column being the hues, the second column being the saturation, and the third column being the value. You probably want the saturation column to be all 1's. Then for each hue, use linspace() to get a series of step values in the value component (the 3rd column). How many hues do you want, and for each hue, how many brightness steps do you want?
  2 Comments
z8080
z8080 on 5 Jul 2016
many thanks. I have 4 different conditions (hues), and the colorbar represents a histogram that counts up to 140, so I guess up to 140 brightness steps. I tried implementing the Guillaume's solution above but it didn't work.
Image Analyst
Image Analyst on 6 Jul 2016
Edited: Image Analyst on 6 Jul 2016
If you post your data, and say how many hues you want and what data value range each hue should dover, and how many darknesses for each hue you want, I might be able to try some things. For example, let's say your data is continuous values between 0 and 90 and you want 0-30 to be different lightnesses of red, 30-60 to be different darknesses of yellow, and 60-90 to be different darknesses of green. And for each hue, you want 4 different darknesses. Or whatever. You have to let me know what the color map should be so I can figure out how to create it.

Sign in to comment.

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!