Plotting a rectangle with colour representing a value in Matlab -
i plot number of rectangles, of have associated value. can plot points values using scatter(x,y,[],value);
rectangle
function not seem have such functionality.
thank you
you can set rectangle color, although not quite direct analog how scatter
. when using rectangle
, have 2 color options; edge color , face color. set edge color, use 3-element vector representing rgb values, such each element within range [0, 1]. e.g.
%make arbitrary rectangle (in case, located @ (0,0) [width, height] of [10, 20]) rect_h = rectangle('position', [0, 0, 10, 20]); %sets edge green set(rect_h, 'edgecolor', [0, 1, 0])
the face color of rectangle fill-color -- can set using color string (e.g. 'g' green, 'r' red, etc) or using 3-element vector in same manner edge color property.
e.g. these 2 commands have same effect:
set(rect_h, 'facecolor', 'r'); set(rect_h, 'facecolor', [1, 0, 0]);
in case, need mapping value (whatever form may in) 3-element rgb color vector. i'm not sure goal coloring, if you're looking have rectangle colors different, use mapping function along lines of:
color_map = @(value) ([mod((rand*value), 1), mod((rand*value), 1), mod((rand*value), 1)])
then have
set(rect_h, 'facecolor', color_map(value));
where value
assumed scalar. also, if you're looking on 1 line akin scatter
can too:
rectangle('position', [x, y, w, h], 'facecolor', color_map(value));
update: have work nicely colorbar
, you'd have save each of 3-element color vectors, , pass matlab built-in function colormap
. call colorbar
. have no idea kind of color mapping you're using, sake of illustration:
figure; hold on; %have 20 rectangles num_rects = 20; %place rectangles in random locations, within [10 x 10] area, %each rectange being of size [1 x 1] random_rectangles = [rand(num_rects, 2)*10, ones(num_rects,2)]; %assign random color mapping each of 20 rectangles rect_colors = rand(num_rects,3); %plot each rectangle i=1:num_rects rectangle('position', random_rectangles(i,:), 'facecolor', rect_colors(i,:)); end %set colormap rectangle colors colormap(rect_colors); %adds colorbar plot colorbar
hopefully that's asking about...
Comments
Post a Comment