Related
I have code and the result as below:
%% How to plot each matrix in a cell in 3d plot(1 matrix with 1 color) ?
% Generate Sample data cell A(1x10 cell array)
clear; clc;
A = cell(1,10); % cell A(1x10 cell array)
for kk = 1:numel(A)
z = 10*rand()+(0:pi/50:10*rand()*pi)';
x = 10*rand()*sin(z);
y = 10*rand()*cos(z);
A{kk} = [x,y,z];
end
A_6 = A(1:6); % generate a cell with the first 6 matrices in "A" cell array
% The numer "6" can be changed to be any number which you want to plot them by colormap
newA = vertcat(A_6{:}); %Concatenating all matrices inside A vertically
numcolors = numel(A_6); %Number of matrices equals number of colors
colourRGB = hsv(numcolors); %Generating colours to be used using hsv colormap
colourtimes = cellfun(#(x) size(x,1),A_6);%Determining num of times each colour will be used
colourind = zeros(size(newA,1),1); %Zero matrix with length equals num of points
colourind([1 cumsum(colourtimes(1:end-1))+1]) = 1;
colourind = cumsum(colourind); %Linear indices of colours for newA
scatter3(newA(:,1), newA(:,2), newA(:,3), [], colourRGB(colourind,:),'filled');
%if you want to specify the size of the circles, use the following line instead:
% scatter3(newA(:,1), newA(:,2), newA(:,3), colourind , colourRGB(colourind,:),'filled');
grid on;
view(3); %view in 3d plane
colormap(colourRGB); %using the custom colormap of the colors we used
%Adjusting the position of the colorbar ticks
caxis([1 numcolors]);
colorbar('YTick',[1+0.5*(numcolors-1)/numcolors:(numcolors-1)/numcolors:numcolors],'YTickLabel', num2str([1:numcolors]'), 'YLim', [1 numcolors]);
I have the image like this:
How can I shown "YTickLabel" with some specified value (not show all) as below figure?
Taking the code from your comment to the previous answer:
h = colorbar('YTick',[1:numcolors],'YTickLabel', num2str([1:numcolors]'), 'YLim', [1 numcolors]); set(h, 'Ticklabels', {1 [] 3 [] 5 6}); set(h, 'Ticks', {1 3 5 6});
You are now setting 6 tick labels for only 4 ticks. If set the ticks to the correct values, then the auto-generated tick labels will be what you want.
Try the following:
h = colorbar('YTick',[1:numcolors],'YTickLabel', num2str([1:numcolors]'), 'YLim', [1 numcolors]);
set(h, 'Ticks', {1 3 5 6});
To remove some ticks with their labels, just set the 'YTick' property of the colorbar accordingly. That is, replace your last line by something like:
colorbar('YTick', [1 3 5 6], 'YLim', [1 numcolors])
Think of a 3 dimensional image V stored, in matlab, as an array for red, green, and blue. For pixel (i,j,k) we would have
V(i,j,k,1)=0.1 (red)
V(i,j,k,2)=0.9 (green)
V(i,j,k,3)=0.2 (blue)
I would then like to visualize this 3D image as a moving 2D slice. I thought the "slice" command could do the trick but it is designed to visualize 3D functions only, not 3D images. Do you have any suggestion?
I am adding a script that could help but does not achieve what is requested:
clear all; clc; close all;
N=35;
w=zeros(N,N,N);
x=linspace(-5,5,N); y=linspace(-5,5,N); z=linspace(-5,5,N);
for i=1:N
for j=1:N
for k=1:N
w(i,j,k)=cos(k)/(1+i+j)^2;
end
end
end
for k = 0:.1:10
hsp = surf(linspace(-5,5,Nx),linspace(-5,5,Ny),zeros(Nz));
rotate(hsp,[1,0,0],18*k,[0 0 0]) %rotate a slice where the image is shown
xd = hsp.XData;
yd = hsp.YData;
zd = hsp.ZData;
delete(hsp)
slice(x,y,z,w,xd,yd,zd)
shading flat
hold off
view(-25,20)
axis([-5 5 -5 5 -5 5]);
drawnow
end
The above w array should now be a 3D image instead.
It sounds like you want the functionality of slice but you want it to operate on an [N x M x P x 3] array instead of an [N x M x P] array.
One way to do this is by setting up an interpolator for each channel:
red = griddedInterpolant({x,y,z},V(:,:,:,1));
green = griddedInterpolant({x,y,z},V(:,:,:,2));
blue = griddedInterpolant({x,y,z},V(:,:,:,3));
and then replacing
slice(x,y,z,w,xd,yd,zd)
with
cdata = cat(3, red(xd,yd,zd), green(xd,yd,zd), blue(xd,yd,zd));
surface(xd,yd,zd,cdata);
I tried this on your example and found that the colors in w were all very close to black. So I loaded a different image to work on:
% An orange-and-black skull for Halloween
load mri
V = permute(double(D), [1 2 4 3]);
V = V / max(V(:));
V = bsxfun(#times, V, reshape([1 .3 0], [1 1 1 3]));
% Define grid coordinates and interpolator
[Nx, Ny, Nz, nCh] = size(V);
x = linspace(-5,5,Nx);
y = linspace(-5,5,Ny);
z = linspace(-5,5,Nz);
red = griddedInterpolant({x,y,z},V(:,:,:,1));
green = griddedInterpolant({x,y,z},V(:,:,:,2));
blue = griddedInterpolant({x,y,z},V(:,:,:,3));
for k = 0:.1:10
hsp = surf(linspace(-5,5,Nx),linspace(-5,5,Ny),zeros(Ny,Nx));
rotate(hsp,[1,0,0],18*k,[0 0 0])
xd = hsp.XData;
yd = hsp.YData;
zd = hsp.ZData;
delete(hsp)
cdata = cat(3, red(xd,yd,zd), green(xd,yd,zd), blue(xd,yd,zd));
surface(xd,yd,zd,cdata)
shading flat
hold off
view(-25,20)
axis([-5 5 -5 5 -5 5]);
drawnow
end
I have a data set that looks like this
140400 70.7850 1
140401 70.7923 2
140402 70.7993 3
140403 70.8067 4
140404 70.8139 5
140405 70.8212 3
Where the first column corresponds to time (one second intervals between data points) and will be on the x axis, the second column corresponds with distance and will be on the y axis. The third column is a number (one through five) that is a qualification of the movement.
I want to make a plot that changes the color of the line between two points depending on what the number of the previous data point was. For example, I want the line to be red between the first and second data points because the qualification value was 1.
I've seen a lot of posts about making a sliding scale of colors depending on an intensity value, but I just want 5 colors: (red, orange, yellow, green, and blue) respectively.
I tried doing something like this:
plot(x,y,{'r','o','y','g','b'})
But with no luck.
Any ideas of how to approach this? Without looping if possible.
You can also do it with a trick which works with Matlab version anterior to 2014b (as far back as 2009a at least).
However, is will never be as simple as you expected (unless you write a wrapper for one of the solution here you can forget about plot(x,y,{'r','o','y','g','b'})).
The trick is to use a surface instead of a line object. Surfaces benefit from their CData properties and a lot of useful features to exploit color maps and texture.
Matlab surf does not handle 1D data, it needs a matrix as input so we are going to give it by just duplicating each coordinate set (for example xx=[x,x]).
Don't worry though, the surface will stay as thin as a line, so the end result is not ugly.
%% // your data
M=[140400 70.7850 1
140401 70.7923 2
140402 70.7993 3
140403 70.8067 4
140404 70.8139 5
140405 70.8212 3];
x = M(:,1) ; %// extract "X" column
y = M(:,2) ; %// same for "Y"
c = M(:,3) ; %// extract color index for the custom colormap
%% // define your custom colormap
custom_colormap = [
1 0 0 ; ... %// red
1 .5 0 ; ... %// orange
1 1 0 ; ... %// yellow
0 1 0 ; ... %// green
0 0 1 ; ... %// blue
] ;
%% // Prepare matrix data
xx=[x x]; %// create a 2D matrix based on "X" column
yy=[y y]; %// same for Y
zz=zeros(size(xx)); %// everything in the Z=0 plane
cc =[c c] ; %// matrix for "CData"
%// draw the surface (actually a line)
hs=surf(xx,yy,zz,cc,'EdgeColor','interp','FaceColor','none','Marker','o') ;
colormap(custom_colormap) ; %// assign the colormap
shading flat %// so each line segment has a plain color
view(2) %// view(0,90) %// set view in X-Y plane
colorbar
will get you:
As an example of a more general case:
x=linspace(0,2*pi);
y=sin(x) ;
xx=[x;x];
yy=[y;y];
zz=zeros(size(xx));
hs=surf(xx,yy,zz,yy,'EdgeColor','interp') %// color binded to "y" values
colormap('hsv')
view(2) %// view(0,90)
will give you a sine wave with the color associated to the y value:
Do you have Matlab R2014b or higher?
Then you could use some undocumented features introduced by Yair Altman:
n = 100;
x = linspace(-10,10,n); y = x.^2;
p = plot(x,y,'r', 'LineWidth',5);
%// modified jet-colormap
cd = [uint8(jet(n)*255) uint8(ones(n,1))].' %'
drawnow
set(p.Edge, 'ColorBinding','interpolated', 'ColorData',cd)
My desired effect was achieved below (simplified):
indices(1).index = find( data( 1 : end - 1, 3) == 1);
indices(1).color = [1 0 0];
indices(2).index = find( data( 1 : end - 1, 3) == 2 | ...
data( 1 : end - 1, 3) == 3);
indices(2).color = [1 1 0];
indices(3).index = find( data( 1 : end - 1, 3) == 4 | ...
data( 1 : end - 1, 3) == 5);
indices(3).color = [0 1 0];
indices(4).index = find( data( 1 : end - 1, 3) == 10);
indices(4).color = [0 0 0];
indices(5).index = find( data( 1 : end - 1, 3) == 15);
indices(5).color = [0 0 1];
% Loop through the locations of the values and plot their data points
% together (This will save time vs. plotting each line segment
% individually.)
for iii = 1 : size(indices,2)
% Store locations of the value we are looking to plot
curindex = indices(iii).index;
% Get color that corresponds to that value
color = indices(iii).color;
% Create X and Y that will go into plot, This will make the line
% segment from P1 to P2 have the color that corresponds with P1
x = [data(curindex, 1), data(curindex + 1, 1)]';
y = [data(curindex, 2), data(curindex + 1, 2)]';
% Plot the line segments
hold on
plot(x,y,'Color',color,'LineWidth',lineWidth1)
end
When the result figure of two variables plotted is a circle, will be necessary to add the time in z axes.
For example the figure of induction machine rotor velocity vs electric torque in one laboratory test is: 2d plot figure
In the last figure the direction of the time point plotting could be clockwise or counter clockwise. For the last reason will be added time in z axis.
% Wr vs Te
x = logsout.getElement( 'Wr' ).Values.Data;
y = logsout.getElement( '<Te>' ).Values.Data;
z = logsout.getElement( '<Te>' ).Values.Time;
% % adapt variables for use surf function
xx = zeros( length( x ) ,2 );
yy = zeros( length( y ) ,2 );
zz = zeros( length( z ) ,2 );
xx (:,1) = x; xx (:,2) = x;
yy (:,1) = y; yy (:,2) = y;
zz (:,1) = z; zz (:,2) = z;
% % figure(1) 2D plot
figure (1)
hs = surf(xx,yy,zz,yy,'EdgeColor','interp') %// color binded to "y" values
colormap('hsv')
view(2)
% %
figure(2)
hs = surf(xx,yy,zz,yy,'EdgeColor','interp') %// color binded to "y" values
colormap('hsv')
view(3)
Finally we can view the 3d form and detect that counterwise is the real direction of the time plotting is: 3d plot
Scatter can plot the color according to the value and shows the colormap of the range of values. It's hard to interpolate the color though if you want continuous curves.
Try:
figure
i = 1:20;
t = 1:20;
c = rand(1, 20) * 10;
scatter(i, t, [], c, 's', 'filled')
colormap(jet)
The figure looks like
For example (code):
x = [3 6 2 9 5 1];
bar(x)
for this I need to add data labels on top of the each bar.
I know that I have to use TEXT keyword, but I'm not getting how to implement it.
Here is a simple solution with text:
x = [3 6 2 9 5 1];
bar(x)
ylim([0 max(x)*1.2])
text(1:numel(x),x+0.5,num2cell(x))
Based off this answer:
data = [3 6 2 9 5 1];
figure; %// Create new figure
hbar = bar(data); %// Create bar plot
%// Get the data for all the bars that were plotted
x = get(hbar,'XData');
y = get(hbar,'YData');
ygap = 0.1; %// Specify vertical gap between the bar and label
ylimits = get(gca,'YLim');
%// The following two lines have minor tweaks from the original answer
set(gca,'YLim',[ylimits(1),ylimits(2)+0.2*max(y)]);
labels = cellstr(num2str(data')) %//'
for i = 1:length(x) %// Loop over each bar
xpos = x(i); %// Set x position for the text label
ypos = y(i) + ygap; %// Set y position, including gap
htext = text(xpos,ypos,labels{i}); %// Add text label
set(htext,'VerticalAlignment','bottom', 'HorizontalAlignment','center')
end
After some attempts I have found the solution. Do the following:
y = Data;
for b = 1 : 10
BarPlot(b) = bar(b, y(b), 'BarWidth', 0.9); % actual plot
set(BarPlot(b), 'FaceColor', 'blue'); %Apply color
barTopper = sprintf('%.1f%s', y(b)*100,'%'); % Place text on top
text(b-0.5, y(b)+0.01, barTopper, 'FontSize', barFontSize); % position the text
hold on;
end
Let me know if it works.
This question already has answers here:
How to create a custom colormap programmatically?
(2 answers)
Closed 7 years ago.
I'd like to create a color palette between two colors. For instance between Blue and Red with 20 or 50 instances.
How can this be achieved in Matlab R2014b?
You can use any kind of interpolation (e.g. interp1) to create your own custom colormap between two colors or multiple colors. A colormap is basically a 3-column matrix with RGB-values. In your case its pretty simple, as you just need red with [1 0 0] and blue [0 0 1] and linearly interpolated in between. linspace is therefore the best choice.
n = 50; %// number of colors
R = linspace(1,0,n); %// Red from 1 to 0
B = linspace(0,1,n); %// Blue from 0 to 1
G = zeros(size(R)); %// Green all zero
colormap( [R(:), G(:), B(:)] ); %// create colormap
%// some example figure
figure(1)
surf(peaks)
colorbar
Note that you could also use the the colormap GUI by typing colormapeditor.
Alternative you can also use 2D-interpolation:
n = 50; %// number of colors
cmap(1,:) = [1 0 0]; %// color first row - red
cmap(2,:) = [0 1 0]; %// color 25th row - green
cmap(3,:) = [0 0 1]; %// color 50th row - blue
[X,Y] = meshgrid([1:3],[1:50]); %// mesh of indices
cmap = interp2(X([1,25,50],:),Y([1,25,50],:),cmap,X,Y); %// interpolate colormap
colormap(cmap) %// set color map
%// some example figure
figure(1)
surf(peaks)
colorbar
And just another example using spline-interpolation to get wider areas of blue and red:
n = 50; %// number of colors
v = [0,0,0.1,0.5,0.9,1,1];
x = [-5*n,0, 0.45*n, 0.5*n, 0.55*n, n, 5*n];
xq = linspace(1,n,n);
vq = interp1(x,v,xq,'spline');
vq = vq - min(vq);
vq = vq./max(vq);
B = vq; %// Blue from 0 to 1 with spline shape
R = fliplr(B); %// Red as Blue but mirrored
G = zeros(size(R)); %// Green all zero
colormap( [R(:), G(:), B(:)] ); %// create colormap
%// some example figure
figure(1)
surf(peaks)
colorbar
Or use any mathematical function you want:
n = 50; %// number of colors
t = linspace(0,4*pi,50);
B = sin(t)*0.5 + 0.5; %// Blue from 0 to 1 as sine
R = cos(t)*0.5 + 0.5; %// Red from 0 to 1 as cosine
G = zeros(size(R)); %// Green all zero
colormap( [R(:), G(:), B(:)] ); %// create colormap
%// some example figure
figure(1)
surf(peaks)
colorbar