plotting diffrent curves in one plot in matlab - matlab

I want to make a plot in Matlab like this.
How could I do something like this in Matlab?
Thank you all!

Make some data:
x = 1:0.1:100;
y = 1:5;
for i = y
z(i,:) = sin(i*x);
end
Plot it:
figure
hold on
for i = y
plot3(x,i*ones(size(x)),z(i,:))
end
Modify the plot aspect and view:
daspect([100,2,2])
view(45,60)
Does that do roughly what you need?

You can use the command plot3(X,Y,Z).
You have to build three matrices each one containing a number of column equal to the number of series you need. (6 in the figure you sent)
For example
X = repmat([-200:200]',1,6);
Z = rand(401,6)*10;
Y = ones(401,1)*[1:20:120];
plot3(X,Y,Z)
axis image

Related

plot a 3D matrix of concentrations in matlab with slice

I have a 3D matrix C=51x51x11 dimensions, obtained from a function in a separate script, the x,y,z represent length, depth and height, and the value represent a concentration per x,y,z point. I want to create a slice crossing x and another crossing y showing the difference in concentration by color. I have tried using ngrid and meshgrid but didn't work. may i have some help with this please?
Use slice()
C = randi(1,[51,51,11]);
x= 25; y = 25; z = 5;
sl = slice(C,x,y,z);
Using slice inside a function to make it easy to view in 3d:
function eslice(V,sx,sy,sz)
slice(V,sx,sy,sz)
shading interp
axis equal
axis vis3d
end
This is from my personal library, enjoy.

how to change size(out) = [m n] in matlab

This is a continuation of my previous problem, so now i want to know how to change array size and how to use size(out) = [m n].
so basically if you have 10x10 array and you want to plot column 9 vs. column 10 in scatter plot, and you want column 1-column 8 to be the labels of your scatter plot. how can i use size(out) = [10 10]?
for someone who want examples:
Auto-Label in scatter plot using matlab
and what if your array is m x n? is there a general code for this? please enlighten me, thanks.
To make things more general you can make use of the end keyword, which refers to the last row/column or an array/cell array/anything in Matlab (actually "last array of index").
Revisiting your example, you could use num2str (alternatively to sprintf) and use the following:
scatter(out(:,end-1), out(:,end));
for k = 1:size(out,1)
T{k} = num2str(out(k,1:end-2));
end
xshift = 0.03; yshift = 0.03;
text(out(:,3)+xshift, out(:,4)+yshift, T);
grid on
Which gives this:

Matlab - Trying to use vectors with grid coordinates and value at each point for a color plot

I'm trying to make a color plot in matlab using output data from another program. What I have are 3 vectors indicating the x-position, y-yposition (both in milliarcseconds, since this represents an image of the surroundings of a black hole), and value (which will be assigned a color) of every point in the desired image. I apparently can't use pcolor, because the values which indicate the color of each "pixel" are not in a matrix, and I don't know a way other than meshgrid to create a matrix out of the vectors, which didn't work due to the size of the vectors.
Thanks in advance for any help, I may not be able to reply immediately.
If we make no assumptions about the arrangement of the x,y coordinates (i.e. non-monotonic) and the sparsity of the data samples, the best way to get a nice image out of your vectors is to use TriScatteredInterp. Here is an example:
% samplesToGrid.m
function [vi,xi,yi] = samplesToGrid(x,y,v)
F = TriScatteredInterp(x,y,v);
[yi,xi] = ndgrid(min(y(:)):max(y(:)), min(x(:)):max(x(:)));
vi = F(xi,yi);
Here's an example of taking 500 "pixel" samples on a 100x100 grid and building a full image:
% exampleSparsePeakSamples.m
x = randi(100,[500 1]); y = randi(100,[500 1]);
v = exp(-(x-50).^2/50) .* exp(-(y-50).^2/50) + 1e-2*randn(size(x));
vi = samplesToGrid(x,y,v);
imagesc(vi); axis image
Gordon's answer will work if the coordinates are integer-valued, but the image will be spare.
You can assign your values to a matrix based on the x and y coordinates and then use imagesc (or a similar function).
% Assuming the X and Y coords start at 1
max_x = max(Xcoords);
max_y = max(Ycoords);
data = nan(max_y, max_x); % Note the order of y and x
indexes = sub2ind(size(data), max_y, max_x);
data(indexes) = Values;
imagesc(data); % note that NaN values will be colored with the minimum colormap value

pcolor in scatter plot matlab

I have a large matrix DAT(50000+,42). I am plotting 2 rows of this matrix on the x and y axes, and want the plot points to vary in color due to the value of a separate row. Can anybody advise? pcolor will not work for me due to "color data input must be a matrix" error.
TIA
X = DAT(:,21);
Y = DAT(:,22);
Z = DAT(:,28);
plot(X,Y,'*');
hold on
pcolor(X,Y,Z);
hold off
You could consider using scatter()
% random sample data
DAT = randn(30,42);
X = DAT(:,21);
Y = DAT(:,22);
Z = DAT(:,28);
scatter(X,Y,50,Z); % x,y,size,color -> size can also be a vector
% scatter(X,Y,50,Z,'*'); % to also change the marker type
You can select the colors from an array generated with colormap like this:
DAT = randn(30,42);
X = DAT(:,21);
Y = DAT(:,22);
Z = DAT(:,28);
[dummy ID]=sort(Z);
colors=colormap(jet(length(Z)));
figure
for i=1:length(Z)
plot(X(i),Y(i),'*','Color',colors(ID(i),:));
hold on
end
The only issue with this technique is that you cannot do plots with millions of points because of the looped plotting, but otherwise it works like a charm:

MATLAB - Plotting multiple graphs

I am new to MATLAB and am having difficulty plotting multiple graphs. Here are my vectors to graph:
S = [1.2421
2.3348
0.1326
2.3470
6.7389
3.7089
11.8534
-1.8708
...]
Y = [1.1718
1.8824
0.3428
2.1057
1.6477
2.3624
2.1212
-0.7971
...]
w = [0.1753
0.3277]
S is my training data and Y is my output vector. Then I add a column vector to my training data:
O = ones(length(S), 1)
X = [S 0]
w = inv(X'*X)*X'*Y
So I am trying to plot X, Y and w on the same graph. I plot w first, hold, X and this is where I get lost. Basically they are not on the same scale because the size of x is much less than X (X and Y are both vectors of size 100 and w is of size 2).
plot(w)
Then I do:
hold
plot(X)
Now the w that I plotted is so small compared to the plot of X. How would I make them the same scale? Also maybe making them a different color?
plotyy will create the figure you are looking for. See the examples in the link for further plot customization.
I'd just comment, but I don't have enough reputation... If you are not aiming to present the data, but just be able to visualize it, you can rescale your datasets and avoid the not-so-easy-to-work-with plotyy (although it is the best answer):
W = W/max(W);
X = X/max(X);
plot(W)
hold on
plot(X)
For additional formating of the plots, see mathworks polt. There you can change color, linewidth and whatnot.