Is there a way to specify the colors of the lines when plotting the matrix.
For instance:
// here is my matrix A
A = [13, 3, 4;19, 0, 1;18, 0, 2;19, 0, 1;19, 0, 1];
// when I am plotting it I am not in control of what color each line will be
plot(A)
Using
plot(A, 'r')
just colors everything in red (which is expected)
When trying something like
plot(A, ['r', 'g','b'])
or
plot(A, 'rgb')
does not work (which is not surprising)
So is there any way to specify color for each line?
You can change the color afterwards:
A = [13 3 4;
19 0 1;
18 0 2;
19 0 1;
19 0 1];
p=plot(A);
clrs = jet(numel(p)); % just a Nx3 array of RGB values
for ii=1:numel(p)
set(p(ii),'color',clrs(ii,:));
end
Example:
A=sin(repmat(linspace(0,2*pi,200),20,1)'*diag(linspace(1,2,20)));
% same thing as above
The plot function doesn't provide a way to do it as concisely as in your example. Instead, you can try:
plot(A(:, 1), 'r', A(:, 2), 'g', A(:, 3), 'b');
Related
I am trying to make a plot with multiple y-values for a single x-value. The code that I have been using plots this in two separate axes.
in = {[26 171 40], [34 32 104 28]}
titles = {'Locker','9u'}
for i = 1:length(in)
subplot(1,length(in), i);
scatter(ones(1,length(in{i})), in{i},'filled')
set(gca,'XTick',[])
xlabel(titles{i});
end
How can I plot my data in a single axes? I am not very experienced at using Matlab.
You can plot at different x locations, by multiplying the vector with ones by i. And then set the xticks, labels and axis limits appropriately.
As for the colors, you can make a length(in)-by-3 matrix containing the rgb values per category of data. Then provide the appropriate row of this matrix to scatter in the loop.
in = {[26 171 40], [34 32 104 28], randi(200, 5,1), randi(150, 3,1)};
titles = {'Locker','9u', 'A', 'B'};
category_colors = [
1, 0, 0; % color for 'Locker'
1, 0, 0; % '9u'
0, 0, 1; % 'A'
0, 0, 1; % 'B'
];
figure(1); clf;
hold on;
for i = 1:length(in)
scatter(ones(1,length(in{i}))*i, in{i},[], category_colors(i,:), 'filled'); % multiply ones with i
end
set(gca,'XTick',1:length(in));
set(gca, 'XTickLabel', titles);
xlim([0.5 length(in)+0.5])
I was thinking of a question similar to this.
I have a function which takes as input the three values x,y,z from the R^3 and returns either 1,2,3,4. Now I wanted to plot the point in the 3D space with coordinates (x,y,z) with a color associated with the functional value at that point which can be either one of 1,2,3 or 4.
I have a 3D matrix with integer entries like say 1,2,3,4 and I store the points value in this matrix so that I can plot the points with the corresponding color (similar trick of 'image' command in MATLAB for making 2D plots).
color coding (say)-
1 - green, 2 - blue , 3 - cyan , 4 -red
Like if at the point (0.5,0.5,0.1) the function returns the value 3, then I mark the point (0.5,0.5,0.1) with the color associated to number three which is cyan.
I am thinking of a MATLAB command which does this in the case of three dimensional case as the "image" command seems to work for the 2D case.
I can only think of some kind of workaround, like this:
% Input: A = coordinates, b = functional values.
A = rand(20, 3);
b = ceil(rand(20, 1) * 4);
% Color map.
cm = [0 1 0; 0 0 1; 0 1 1; 1 0 0];
% Circle size.
cs = 21;
% 3D scatter plot.
figure(1);
hold on;
for k = 1:size(cm, 1)
idx = (b == k);
scatter3(A(idx, 1), A(idx, 2), A(idx, 3), cs, cm(k, :), 'filled');
end
hold off;
view(45, 30);
grid on;
Gives the following output:
You can linearize the solution suggested by #HansHirse, so a small improvement could be:
% Dummy data
A = rand(20, 3);
b = ceil(rand(20, 1) * 4);
% color vector
c = [0 1 0; 0 0 1; 0 1 1; 1 0 0];
% Use the linear indexing to select the right color
scatter3(A(:,1),A(:,2),A(:,3),[],c(b,:),"filled")
Even simpler you can just use b as color input and matlab will use the default colormap to set the color according to b
scatter3(A(:,1),A(:,2),A(:,3),[],b,"filled")
I'm trying to create a figure in matlab that looks like this:
desired figure
I am doing so by: (i) assigning value points to each x,y coordinate, (ii) plotting a surf, and (iii) change the view point so the third axis is not seen. Here is the code:
x = linspace(0, 1, 10);
y = linspace(0, 1, 10);
z = linspace(0, 1, 10);
z = repmat(z, 10, 1);
z = flipud(triu(z));
z(z==0) = nan;
hold off
surf(x, y, z, 'linestyle', 'none')
colormap([linspace(0.39, 1, 20)',linspace(0.58, 0.25, 20)', linspace(0.93, 0.25, 20)']);
colorbar
xlim([x(1) x(end)])
shading interp
view([90 -90])
hold on
plot(x, 1-y, 'linewidth', 2)
I get the following figure: matlab figure I get
As you can see, there a lot of white spaces above the line which I would like to be in color as well. Unfortunately, I cannot add any more grid points as calculating the actual value of the points takes a lot of time (unlike the example above).
Is there a way to have matlab draw colors in those white spaces as well?
Thanks!
You can try to use patch function to create filled polygon.
See http://www.mathworks.com/help/matlab/ref/patch.html
Try the following code:
vert = [0 1;1 1;1 0]; % x and y vertex coordinates
fac = [1 2 3]; % vertices to connect to make triangle
fvc = [1 0 0; 1 1 1; 0 0 1];
patch('Faces',fac,'Vertices',vert,'FaceVertexCData',fvc,'FaceColor','interp');
Result is close:
I was managed to get closer to the desired figure:
close all
x = linspace(0, 1, 10);
y = linspace(0, 1, 10);
%colorbar
xlim([x(1) x(end)])
%Fill rectangle.
vert = [0 0; 1 0; 1 1; 0 1]; % x and y vertex coordinates
fac = [1 2 3 4]; % vertices to connect to make squares
%patch('Faces',fac,'Vertices',vert,'FaceColor','red')
fvc = [1 0 0; 0.6 0.7 1; 0.6 0.7 1; 1 0 0]; %Color of vertices (selected to be close to example image).
patch('Faces',fac,'Vertices',vert,'FaceVertexCData',fvc,'FaceColor','interp')
hold on
%Fill lower triangle with white color.
vert = [0 0;0 1;1 0]; % x and y vertex coordinates
fac = [1 2 3]; % vertices to connect to make triangle
fvc = [1 1 1; 1, 1, 1; 1, 1, 1]; %White color
patch('Faces',fac,'Vertices',vert,'FaceVertexCData',fvc,'FaceColor','interp');
plot(x, 1-y, 'linewidth', 2)
set(gca,'Xtick',[],'Ytick',[]); %Remove tick marks
Result:
Thank you Rotem! I wasn't aware of the patch function and indeed it solved the issue!
The colors on the actual figure I'm trying to achieve are not linear, so I just used patch for all the empty triangles. Here is the adjusted code I use for the simple example (again, this is just a bit more general just to be able to have non linear colors in the area above the curve):
x = linspace(0, 1, 10);
y = linspace(0, 1, 10);
z = linspace(0, 1, 10);
z = repmat(z, 10, 1)+0.1;
z = flipud(triu(z));
z(z==0) = nan;
z = z-0.1;
hold off
surf(x, y, z, 'linestyle', 'none')
colormap([linspace(0.39, 1, 20)',linspace(0.58, 0.25, 20)', linspace(0.93, 0.25, 20)']);
colorbar
xlim([x(1) x(end)])
shading interp
view([90 -90])
hold on
patch_cor_y = kron((length(y):-1:1)', ones(3, 1));
patch_cor_x = kron((1:length(x))', ones(3, 1));
patch_cor = [y(patch_cor_y(2:end-2))', x(patch_cor_x(3:end-1))'];
patch_path = reshape(1:length(patch_cor),3, length(patch_cor)/3)';
patch_col = z(sub2ind(size(z), patch_cor_x(3:end-1), patch_cor_y(2:end-2)));
patch('Faces',patch_path,'Vertices',patch_cor,'FaceVertexCData',patch_col,'FaceColor','interp', 'EdgeColor', 'none');
plot(x, 1-y, 'linewidth', 2)
The figure achieved: figure
I have data that is constructed as a ratio, and therefore I'd like to use a red/white/blue colormap where white is assigned to 1 and red and blue surround it. Is there an easy way to do this?
You can easily construct a blue-white-read colormap using interp1. Here's a little anonymous function which takes an integer as input and constructs a colormap.
bwr = #(n)interp1([1 2 3], [0 0 1; 1 1 1; 1 0 0], linspace(1, 3, n), 'linear')
colormap(bwr(64));
To control where the white calls, you'll want to set the CLim of the axes. You will want to center your clims around the value that you want to be white.
set(gca, 'clim', [0 2])
And as a full example:
data = reshape(linspace(0, 100, 16), [4 4]) ./ 50;
figure
imagesc(data);
colormap(bwr(64));
colorbar;
set(gca, 'clim', [0 2])
Keep in mind though that since you have a ratio, all of the values between 0 and 1 will be compressed whereas the values > 1 can theoretically go all the way to infinity.
As an example:
data = reshape(linspace(1, 200, 16), [4 4]) ./ 50;
To correct for this you could log-transform your data prior to plotting and then change the colorbar tick marks to represent your initial ratio values.
LData = log(data);
imagesc(LData)
colormap(bwr(64));
cbar = colorbar();
set(gca, 'clim', [-1 1] * max(abs(LData(:))))
cticks = get(cbar, 'Ticks');
cticklabels = arrayfun(#(x)sprintf('%0.2f', x), exp(cticks), 'uniformoutput', false);
set(cbar, 'TickLabels', cticklabels);
This question already has answers here:
How can I plot a 3D-plane in Matlab?
(4 answers)
Closed 8 years ago.
I would like to draw different triangles by using MATLAB shown in the figure below. Suppose that I have 3 vectors V1=[1 1 1], V2=[-1 1 1], v3=[-2 -2 -2].
How can I draw triangle with these vectors in 3D?
![enter image description here][1]
You can use plot3() like this:
v1=[1 1 1]; v2=[-1 1 1]; v3=[-2 -2 -2];
triangle = [v1(:), v2(:), v3(:), v1(:)];
plot3(triangle(1, :), triangle(2, :), triangle(3, :))
xlabel('x'); ylabel('y'); zlabel('z');
This is the output:
Edit:
This is in order to plot axis:
val = 5; %// Max value of axis
axX = [0 0 0; val 0 0];
axY = [0 0 0; 0 val 0];
axZ = [0 0 0; 0 0 val];
plot3(axX(:, 1), axX(:, 2), axX(:, 3), 'k');
plot3(axY(:, 1), axY(:, 2), axY(:, 3), 'k');
plot3(axZ(:, 1), axZ(:, 2), axZ(:, 3), 'k');
text(val, 0, 0, 'x')
text(0, val, 0, 'y')
text(0, 0, val, 'z')
view(3)
In addition you can make the plot look like your reference image, adding these commands to above code:
set(gca,'xtick',[], 'xcolor', 'w')
set(gca,'ytick',[], 'ycolor', 'w', 'YDir','reverse')
set(gca,'ztick',[], 'zcolor', 'w')
view(45, 30)
This is the result: