"Transition-FaceColor" in Matlab - matlab

I want to color a rectangle in Matlab with a "transition facecolor" (I do not know the right term) meaning for instance a transition from hell blue to dark blue; you could also interpret it as a shadowing (here you can see an example:
http://il1.picdn.net/shutterstock/videos/620653/thumb/1.jpg?i10c=img.resize(height:160)
I could imagine to achieve it by using a colormap, but I do not know how to apply it on a text annotation like a rectangle.
Is it possible to modify Matlab's standard (monochromatic) colors in such a way? And if so, does someone have a basic framework for it?

You can create the rectangle using a patch which allows having interpolated face colors.
Then, in order to have the face color ranging from dark blue to bright blue you have to define your own "blue" colormap.
The colormap should be defined as a (N x 3) RGB array: in your case you have to set to 0 the first two columns (corresponding to red and green and have the values of the third column (the blue) ranging from (start_blue,end_blue) where start_blue is the darkest blue level you want, end_blue the brightest (both have to be between 0 and 1).
% Define the rectangle: lower left x, lower left y, width, height
x_rect=1;
y_rect=1;
width=10;
height=5;
% Define the patch vertices and faces
verts=[x_rect y_rect;x_rect y_rect+height; ...
x_rect+width y_rect+height;x_rect+width y_rect];
faces=[1 2 3 4];
% Define the color: the higher the brighter
col=[0; 0; 4; 4];
figure
% Create the new blue colormap
b=0.7:.01:1;
cm1=[zeros(length(b),2) b']
% Set the new colormap
colormap(cm1)
% Plot the patch
patch('Faces',faces,'Vertices',verts,'FaceVertexCData',col,'FaceColor','interp');
As an alternative, you can create the rectangle as a surf, then, define your own colormap as above.
% Define the rectangle:
x_rect=1;
y_rect=1;
width=10;
height=5;
% Build a patch
xp=[x_rect:x_rect+width];
yp=[y_rect:y_rect+height];
% Get the number of points
n_xp=length(xp);
n_yp=length(yp);
% Create the grid
[X,Y]=meshgrid(xp,yp);
% Define the z values
Z=ones(size(X));
% Create the color matrix as uniformly increasing
C=repmat(linspace(1,10,n_xp),n_yp,1)
% Create the new blue colormap
start_blue=0.5;
end_blue=1;
b=start_blue:.01:end_blue;
cm1=[zeros(length(b),2) b']
% Set the new colormap
colormap(cm1)
% Plot the rectangle as a "surf"
surf(X,Y,Z,C)
shading interp
xlabel('X Axis')
ylabel('Y Axis')
view([0 90])
xlim([0 13])
ylim([0 9])
daspect([1 1 1])
Hope this helps.
Qapla'

Related

How to create a color gradient using a third variable in Matlab? [duplicate]

This question already has answers here:
Change color of 2D plot line depending on 3rd value
(5 answers)
Closed 5 years ago.
How do you create a color gradient in Matlab such that you plot a 2D line plot of y=y(x), and you color it using another variable that also depends on x such that z=z(x). A scatter or point plot is also fine by me.
I would also like to have a colormap legend kind of thing showing the color gradient and it's actual representation of z. This stuff is quite common in visualisation tools such as VisIt and ParaView but I could not yet FIGURE it out in Matlab.
If a scatter plot is fine, you can use the 4th input to scatter:
x = -10:0.01:10;
y = sinc(x);
z = sin(x);
scatter(x,y,[],z,'fill')
where z is the color.
The only way I know of to do this is with a little trick using surf:
% Create some sample data:
x = cumsum(rand(1,20)); % X data
y = cumsum(rand(1,20)); % Y data
z = 1:20; % "Color" data
% Plot data:
surf([x(:) x(:)], [y(:) y(:)], [z(:) z(:)], ... % Reshape and replicate data
'FaceColor', 'none', ... % Don't bother filling faces with color
'EdgeColor', 'interp', ... % Use interpolated color for edges
'LineWidth', 2); % Make a thicker line
view(2); % Default 2-D view
colorbar; % Add a colorbar
And the plot:
To manipulate the color of the line continuously, you'll want to use surface.
While at first look, this function looks most useful for plotting 3d surfaces, it provides more flexibility for line coloring than the basic plot function. We can use the edges of the mesh to plot our line, and take advantage of the vertex colors, C, to render interpolated color along the edge.
You can check out the full list of rendering properties, but the ones you are most likely to want are
'FaceColor', 'none', do not draw the faces
'EdgeColor', 'interp', interpolate between vertices
Here's an example adapted from MATLAB Answers post
x = 0:.05:2*pi;
y = sin(x);
z = zeros(size(x)); % We don't need a z-coordinate since we are plotting a 2d function
C = cos(x); % This is the color, vary with x in this case.
surface([x;x],[y;y],[z;z],[C;C],...
'FaceColor','none',...
'EdgeColor','interp');
Generate a colormap, e.g. jet(10). The example would generate a 10*3 matrix.
Use interp1 to interpolate between the values in the RGB space using your data by setting the first color as the lowest value and the last color as the highest value. This would generate a n*3 matrix where n is the number of data points.
Use scatter with the optional parameter c to plot the points with the interpolated colors.
activate colorbar to show the colorbar.

Matlab: How to set color of legend in a scatter plot where each data point gets a different color?

Here is the sample code:
x = rand(100,1);
y = rand(100,1);
score = rand(100,1);
figure;
colormap(flipud(bone));
caxis([0 1])
axis([0 1 0 1])
scatter(x,y,50,score,'+','LineWidth',2);
legend('scores');
I'm using the reversed 'bone' colormap which assigns pure white to score value 0 and pure black to score value 1. However, the legend seems to be automatically assigned the score 0, and so if you run the code the legend color is pure white and not visible.
Is there any ways to fix that? Thanks.
If you only want to plot a black + without showing the color range of the data (as with color bar) you can create a dummy legend for that. Here is how you do that:
% plot some dummy data for the legend:
scatter(nan,nan,[],1,'+','LineWidth',2)
hold on
% plot your data:
scatter(x,y,50,score,'+','LineWidth',2);
hold off
% add the legend only for the first (dummy) data:
leg = legend('scores');
The result:
What you are looking for is a colorbar. This shows a bar with color gradient for all values in the colormap used.
Replace legend('scores'); line with colorbar.
P.S. legend is meant for identifying multiple plots in the same figure.

how to fill gradient color from light yellow to dark yellow?

With matlab, I don't know how patch can make gradient color vertically in this picture. Here I just want a simple color gradient. I have used bwboundaries to catch the edge function and fill pure color just like this:
for i=1:4
input=imread(['heartspline2_4_',num2str(i)],'bmp');
figure,imshow(input);
BW=im2bw(input,graythresh(input));
[B,L]=bwboundaries(BW,'noholes');
for k=1:length(B)
boundary=B{k};
plot(boundary(:,2),boundary(:,1),'k','LineWidth',2);
fvc=[1 1 0;1 0 0;0 0 1];
hold on;
axis off;
if (k==1)
patch(boundary(:,2),boundary(:,1),'w');
else
p=patch(boundary(:,2),boundary(:,1),'y');
end
end
saveas(gca,['y_','heartspline2_4_',num2str(i)],'bmp')
close(gcf)
end
Following Shai's code in his answer in your other question:
%% Load image %%
close all; clear all; clc;
img = imread('https://i.stack.imgur.com/yO8Nd.jpg'); %// read image
bw = img(:,:,1) > 128; %// convert to binary mask
lb = bwlabel(bw,4); %// extract distinct regions
%%
%% Create as many colors as needed
% example: 2
cmap=rand(2,3); % make this yellow if needed
% lets convert to HSV, we can tune the intesity of the image better here
cmap=rgb2hsv(cmap);
% for each color, lets crate a set of colors.
for ii=1:size(cmap,1);
colors{ii}= [cmap(ii,1)*ones(1,size(img,2)); cmap(ii,2)*ones(1,size(img,2)); linspace(0.3,1,size(img,2))].';
% Modify the limits of linspace
% to achieve control over the limits
end
% Now we have the colors, lets create an image of vertical colors and mask
% it
cimage=zeros(size(img,1),size(img,2),3); % empthy color image
finalimage=cimage;
for ii=1:size(colors,2)
colors{ii}=hsv2rgb(colors{ii});
cimage=permute(reshape(repmat(colors{ii},[size(img,1),1,1]),[size(img,2),size(img,1),3]),[2,1,3]); % there is probably a simpler way
finalimage=finalimage+cimage.*repmat((lb==ii),[1 1 3]);
end
figure; imshow(finalimage, [], 'border', 'tight');
If you understand the code properly, you will be able to do it for vertical gradient. I accidentally did horizontal, but should be alright. The code is commented, but do not hesitate to ask. The steps are the following
Create random colors, as many as desired. In this case 2
convert to HSV
Create a range of V values, that represent "light"
Repeat that list of colors for every row on the image
mask it with the labels to just add it to the areas of that label

Plotting a rectangle with colour representing a value in Matlab

I would like to plot a number of rectangles, all of which have an associated value. I can plot points with values using scatter(x,y,[],value); but the rectangle function does not seem to have such functionality.
Thank you
You can set the rectangle color, although not quite as a direct analog to how you do with scatter. When using rectangle, you have two color options; the edge color and the face color. To set the edge color, use a 3-element vector representing the RGB values, such that each element is within the range [0, 1].
e.g.
%make some arbitrary rectangle (in this case, located at (0,0) with [width, height] of [10, 20])
rect_H = rectangle('Position', [0, 0, 10, 20]);
%sets the edge to be green
set(rect_H, 'EdgeColor', [0, 1, 0])
The face color of the rectangle is its fill-color -- you can set that by using a color string (e.g. 'g' is green, 'r' is red, etc) or by using a 3-element vector in the same manner as the edge color property.
e.g. these 2 commands will have the same effect:
set(rect_H, 'FaceColor', 'r');
set(rect_H, 'FaceColor', [1, 0, 0]);
In your case, you would just need some mapping for your value (whatever form it may be in) to a 3-element RGB color vector. I'm not sure what your goal is for the coloring, but if all you're looking for it to have all the rectangle colors be different, you could use some mapping function along the 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 is assumed to be a scalar. Also, if you're looking to do everything on one line akin to scatter you can do that too:
rectangle('Position', [x, y, w, h], 'FaceColor', color_map(value));
UPDATE:
To have this work nicely with colorbar, you'd have to save each of your 3-element color vectors, and pass it to the matlab built-in function colormap. Then call colorbar. I have no idea what kind of color mapping you're using, so just for the sake of illustration:
figure;
hold on;
%have 20 rectangles
num_rects = 20;
%place your rectangles in random locations, within a [10 x 10] area, with
%each rectange being of size [1 x 1]
random_rectangles = [rand(num_rects, 2)*10, ones(num_rects,2)];
%assign a random color mapping to each of the 20 rectangles
rect_colors = rand(num_rects,3);
%plot each rectangle
for i=1:num_rects
rectangle('Position', random_rectangles(i,:), 'FaceColor', rect_colors(i,:));
end
%set the colormap for your rectangle colors
colormap(rect_colors);
%adds the colorbar to your plot
colorbar
Hopefully that's what you were asking about...

Matlab Ploting with different color for iso-surface

I was trying use the code shown below to plot in such a way that each iso-surface will be different in color and there will be a color bar at the right. I made a ss(k) color matrix for different colors. Number of iso-surfaces is 10 but I have only 8 colors. That's why I wrote ss(9)='r' and ss(10)='r'.
I need a solution to plot the iso-surface with different color and bar at the right side.
ss=['y','m','c','r','g','b','w','k','r','r']
k=1;
for i=.1:.1:1
p=patch(isosurface(x,y,z,v,i));
isonormals(x,y,z,v,p)
hold on;
set(p,'FaceColor',ss(k),'EdgeColor','none');
daspect([1,1,1])
view(3); axis tight
camlight
lighting gouraud
k=k+1;
end
Another possibility is to draw the patches with direct color-mapping (by setting the property 'CDataMapping'='direct'), while assigning the 'CData' of each patch to an index in the colormap of your choice. This is in fact recommended for maximum graphics performance.
Consider the following example:
%# volumetric data, and iso-levels we want to visualize
[x,y,z,v] = flow(25);
isovalues = linspace(-2.5,1.5,6);
num = numel(isovalues);
%# plot isosurfaces at each level, using direct color mapping
figure('Renderer','opengl')
p = zeros(num,1);
for i=1:num
p(i) = patch( isosurface(x,y,z,v,isovalues(i)) );
isonormals(x,y,z,v,p(i))
set(p(i), 'CData',i);
end
set(p, 'CDataMapping','direct', 'FaceColor','flat', 'EdgeColor','none')
%# define the colormap
clr = hsv(num);
colormap(clr)
%# legend of the isolevels
%#legend(p, num2str(isovalues(:)), ...
%# 'Location','North', 'Orientation','horizontal')
%# fix the colorbar to show iso-levels and their corresponding color
caxis([0 num])
colorbar('YTick',(1:num)-0.5, 'YTickLabel',num2str(isovalues(:)))
%# tweak the plot and view
box on; grid on; axis tight; daspect([1 1 1])
view(3); camproj perspective
camlight; lighting gouraud; alpha(0.75);
rotate3d on
I also included (commented) code to display the legend, but I found it to be redundant, and a colorbar looks nicer.
Matlab usually plots different iso-surfaces in different colors automatically, so you don't need to care about that. What kind of bar do you need? A colorbar or a legend? Either way, it is just to use the colorbar or legend function..
%Create some nice data
[x y z] = meshgrid(1:5,1:5,1:5);
v = ones(5,5,5);
for i=1:5
v(:,:,i)=i;
end
v(1:5,3:5,2)=1
v(1:5,4:5,3)=2
%Plot data
for i=1:5
isosurface(x,y,z,v,i)
end
%Add legend and/or colorbar
legend('one','Two','Three','Four')
colorbar
Since the color bar encodes value->color, it is impossible to do what you ask for, unless there is no intersection in z-values between all pairs of surfaces. So the solution below assumes this is the case. If this is not the case, you can still achieve it by adding a constant value to each surface, so to separate the surfaces along the z axis, and eliminate any intersection.
The solution is based on constructing a colormap matrix of piecewise constant values, distributed similarly to the z values of your surfaces. So for example, if you have 3 surfaces, the first has z values between 1 and 10, the 2nd between 11 and 30, and the 3rd between 31 and 60, you should do something like this (I plot in 2D for simplicity)
r = [1 0 0];
g = [0 1 0];
b = [0 0 1];
cmap = [r(ones(10,1),:); g(ones(20,1),:); b(ones(30,1),:)];
z1 = 1:10;
z2 = 11:30;
z3 = 31:60;
figure; hold on
plot(z1,'color',r)
plot(z2,'color',g)
plot(z3,'color',b)
colorbar
colormap(cmap)
More complex colormaps (i.e, more colors) can be constructed with different mixtures of red, green, and blue (http://www.mathworks.com/help/techdoc/ref/colorspec.html)