This question already has an answer here:
How to create 3D-Plot in MatLab?
(1 answer)
Closed 8 years ago.
I have just started MATLAB. So please help me.
If I have a m*n matrix.
I want to plot a 3-d plot with x-axis and y- axis as x indices and y indices respectively. And on the z axis, element at i,j in the matrix.
How to plot it in MATLAB?
Say your m*n matrix is A
You can plot the data as a surface by calling
figure %# opens a new figure, otherwise you'll overwrite an existing one
surf(A)
If you want to add x- and y- indices
surf(xIndices, yIndices, A)
If you want a scatter plot, you need to create arrays of the same size as A for the coordinates first
[xx,yy] = meshgrid(xIndices, yIndices);
plot3(xx(:), yy(:), A(:), 'o');
or
scatter3(xx(:), yy(:), A(:))
Related
In Matlab, I have a matrix M with values M(i,j). Each M(i,j) represents the height at the coordinates (i,j). I would like to visualize this landscape.
How can I visualize in 3d the plot with axes (i,j,M(i,j)) ?
You can use for example:
mesh(M)
%or
meshc(M)
%or
bar3(M)
and others.
Hope the title gave an adequate description of my problem. Basically, I am generating a contour plot in MATLAB using the contourf (x,y,z) function, where x and y are vectors of different lengths and z is a matrix of data with dimensions of x times y. The contourf plot is fine, however, I am looking to overlay this plot with the actual data points from the matrix z. I have tried using the scatter function, but I am getting an error message informing me that X and Y must be vectors of the same length - which they're not. Is there any other way to achieve this?
Thanks in advance for any help/suggestions!
I think meshgrid should help you.
z = peaks; %// example 49x49 z data
x = 1:20;
y = 1:49;
z = z(y,x); %// make dimensions not equal so length(x)~=length(y)
[c,h] = contourf(x,y,z);
clabel(c,h); colorbar;
[xx,yy]=meshgrid(x,y); %// this is what you need
hold on;
plot(xx,yy,'k.'); %// overlay points on contourf
Notice plot suffices instead of scatter. If you insist, scatter(xx(:),yy(:),10), for example, does the trick. Although my example isn't particularly interesting, this should hopefully get you started toward whatever you're going for aesthetically.
I want to create a 3x3 matrix of plots in MATLAB where each element is a subplot of some corresponding 1000 samples (suppose values are stored in a 3x3x1000 matrix A). I want the subplots to be spaced very close together, and the y axis numbers/labels to only show up for the left subplots and the x axis numbers/labels to only show up for the bottom subplots.
For example, something similar to plotmatrix, except subplot ij would plot A(i,j,:).
plotmatrix example
Is there an easy way to do this in MATLAB? Thanks!
The axes properties of the subplots can be modified to achieve this, but an easier way would be to use a FEX submission called panel. Have a look at its example output:
subplot can do that for you in MATLAB.
h = subplot(3,3,1)
Will split the current figure in a 3 x 3 matrix and creates an axes(area where you plot something) in the 1st cell of the matrix. h is the "handle" to the axes which you can then use to modify the xlabels and ylabels in any way.
Documentation for subplot
Documentation for Axes
This question already has answers here:
Plotting 3-D Matrix *values* in MATLAB
(2 answers)
Closed 7 years ago.
Ive created a 3d matrix in MATLAB. The values of the matrix are the velocity at that point in a rectangular section. I would like a plot with colours showing the values at each position, is this possible?
Phrasing this another way, I have a matrix of size 100x100x200. Id like a graph that has 100x100x200 points and the colour of each of those points is related to its value.
This question is very similar to this question. You might want to check it out.
UPDATE:
Suppose you have a 3D matrix A:
A = rand(100,100,200);
You want to plot each entry of A mapped to a color at its 3D coordinates. First generate the coordinates:
[x,y,z] = meshgrid(1:100,1:100,1:200);
Now you are ready to use scatter3:
scatter3(x(:),y(:),z(:),5,A(:))
Here the : indexing vectorizes the coordinates column-wise.
Hope this helps.
I have an Nx3 matrix in matlab and I'd like to make a 3 dimensional bar graph out of it, where the X and Y axes are determined by the values of first and second columns of the matrix, the height of each bar is the third column in the matrix, and the number of bars is determined by N.
In other words, if "data" is the matrix then:
data(:, 1) % values of X-axis
data(:, 2) % values of Y-axis
data(:, 3) % values of each Z-axis bar
and there should be one bar for each 1:length(data)
How can I do this in MATLAB?
Secondly, as a variant of this, how can I do the same thing, but this time histogram the bars into N bins in each X, Y, Z dimension? I.e. instead of a bar for each point, just histogram the data into those bins in every dimension, and plot a bar for each bin.
thanks very much for your help.
Regarding your 1st question, you can achieve something similar to your request, by:
stem3 (data(:,1), data(:,2), data(:,3), 'marker', 'none', 'linewidth',10)
Not exactly bars, but produces a similar effect.
To plot 'real' bars (such as the ones bar3 plots), I think you'll have to use low-level graphics function such as surface (which is used by bar3 to plot bars).
Regarding your 2nd question, I'm not sure I understand --- if you calculate a histogram for each dimension, you end up with 4-dimensional data --- the bin location for each dimension + the hist count itself. What exactly do you want to plot?