How to plot a 3d surface graph in MATLAB? - matlab

I have a dataset like so:
| 0.1 0.2 0.3 0.4
----------------------
1 | 10 11 12 13
2 | 11 12 13 14
3 | 12 13 14 15
4 | 13 14 15 16
I want to plot a 3D surface graph in matlab such that the column headings will be on the y axis, the row headings will be on the x axis and the remaining values will determine the height of the point on the z axis.
I have had a look around at lots of different example and I can't work out how to achieve this. At the moment I have got the following:
Y = [0.1 0.2 0.3 0.4];
X = [1 2 3 4];
Z = [10 11 12 13; 11 12 13 14; 12 13 14 15; 13 14 15 16];
Please could someone help me out?

surf(X,Y,Z)

May a bar plot yield the desired picture?
Y = [0.1 0.2 0.3 0.4];
X = [1 2 3 4];
Z = [10 11 12 13; 11 12 13 14; 12 13 14 15; 13 14 15 16];
figure;
bar3(Z)
set(gca(gcf), 'xticklabel',{'0.1','0.2','0.3','0.4'})

Related

How to create a contourf plot from a table?

As far as I understand the way to 3-d plot/ surface plot is "meshgrid".
But the data I have has a specific format:
X
Y
Z
1
0.1
10
1
0.2
12
1
0.3
13
2
0.1
11
2
0.2
12
2
0.3
14
3
0.1
11
3
0.2
12
3
0.3
15
The first and second column (X and Y) repeat themselves in that fashion, and I need to plot Z(X,Y).
How Do I do it?
X = 1:1:3 % grid can be set by beginning:step:end
Y = 0.1:0.1:0.3
[x,y] = meshgrid(X,Y); % then make 2d domain
% z values have to be done manually or can automate if you read your data from txt file
z = [10 12 13; 11 12 14; 11 12 15];
% and finally
surf(x,y,z)

MATLAB - Error bars separation distance and height

I have data arrays w and x; I want to plot error bars y distance apart and z distance above and below the points. Is there a way to do this? I've tried manipulating the errorbar function but can't figure it out.
w [1
3
5
8
9
15
17
34
67
79
90
123
63
23
2
]
x[1
2
3
4
5
6
7
8
9
10
11
12
13
14
15]
plot(x,w)hold on;
errorbar(x,w....not sure what to put after);
I'm trying to plot error bars every 3rd point and with a height of +-5
You could simply draw the error bars yourself
for idx = 1:3:length(w)
plot([x(idx) x(idx)],[w(idx)+5 w(idx)-5]);
end
Alternatively you could give a handle to the errorbar function, but I'm not sure if it allows you to modify this stuff.
By setting the right Properties of the errorbarobject you can get what you need.
Note the LData and UData properties, which are used to specify the height below and above the bars as well as the XData and YData.
clear
clc
close all
w = [1 3 5 8 9 15 17 34 67 79 90 123 63 23 2 ];
x = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
%// Set location on x axis
loc = 1:3:numel(w);
plot(x,w)
hold on;
hErr = errorbar(loc,w(loc),'rx','LData',5,'UData',5,'XData',loc,'YData',w(loc));
Output:

Understanding 3D surface plot

As in this link, I have:
| 0.1 0.2 0.3 0.4
----------------------
1 | 10 11 12 13
2 | 11 12 13 14
3 | 12 13 14 15
4 | 13 14 15 16
Y = [0.1 0.2 0.3 0.4];
X = [1 2 3 4];
Z = [10 11 12 13; 11 12 13 14; 12 13 14 15; 13 14 15 16];
I plotted the surface Z using the command "surf(X,Y,Z)" in matlab. I got:
But really I don't understand the plotted surface. Can someone explain to me in details (in a text) what happens in this surface? For example: how can we observe the point (2,0.2,12)?
Include some labels and a colorbar and everything should be clear:
Y = [0.1 0.2 0.3 0.4];
X = [1 2 3 4];
Z = [10 11 12 13; 11 12 13 14; 12 13 14 15; 13 14 15 16];
surf(X,Y,Z)
colorbar
xlabel('X')
ylabel('Y')
zlabel('Z')
As suggested in the comments you can find your point on the surface by adding:
hold on;
plot3(2,0.2,12,'ro','MarkerSize',10,'MarkerFaceColor','r');
it then appears as a red dot.
Your table contains 16 points, these are plotted and the area inbetween, colored according to the applied colormap with the lowest z-value of the group of 4, which is according to the doc the surface height.
Actually it would be cleaner coding if you'd include the following line before the plot:
[X,Y] = meshgrid(X,Y);
this way all your input variables get the same dimensions:
X =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Y =
0.1 0.1 0.1 0.1
0.2 0.2 0.2 0.2
0.3 0.3 0.3 0.3
0.4 0.4 0.4 0.4
Z =
10 11 12 13
11 12 13 14
12 13 14 15
13 14 15 16
In case of surf the function does that for you, but other plotting functions are may not that tolerant.

Aligning face data from MATLAB triangulation

I have a function f(x,y) which has certain symmetries that I would like to plot. Here is an example:
This plot can be generated with:
[x,y,z] =
0 0 0.1415
0.1999 0.1999 0.1165
0.2760 0 0.1268
0.3694 0.3694 0.0983
0.4830 0 0.1142
0.5090 0.5090 0.0903
0.5550 0.1871 0.0881
0.6189 0.3558 0.0715
0.6197 0.6197 0.0907
0.6399 0 0.1056
0.7071 0.7071 0.1415
0.7169 0.4835 0.0869
0.7215 0.1200 0.0859
0.7304 0.2392 0.0680
0.7643 0 0.1005
0.7926 0.3574 0.0856
0.8090 0.5878 0.1393
0.8581 0.1122 0.0821
0.8634 0.2343 0.0878
0.8794 0 0.0986
0.8910 0.4540 0.1332
0.9511 0.3090 0.1253
0.9877 0.1564 0.1191
1.0000 0 0.1169
t =
6 4 8
12 6 8
8 4 7
4 2 7
8 7 14
14 7 13
3 2 1
5 7 3
3 7 2
17 12 21
6 12 9
9 17 11
12 17 9
10 15 13
10 7 5
13 7 10
21 12 16
16 12 8
8 14 16
18 14 13
15 20 18
13 15 18
24 23 18
18 20 24
21 16 22
23 22 19
19 18 23
14 18 19
19 16 14
19 22 16
trisurf(t,x,y,z)
So I know that function has a reflection symmetry about y=x and then the resulting function is to be repeated in all the quadrants. Here is the code to do this:
allx = [x; x;-x;-x;y; y;-y;-y];
ally = [y;-y; y;-y;x;-x; x;-x];
allz = [z; z; z; z;z; z; z; z];
These are the new vertices for the surface I want to plot. Now how do I properly generate the faces for this new surface?
When I use a finer mesh and add some pretty lights it should look something like this:
Speculative:
So your question is about how to set-up the first argument of trisurf, i.e. how to define the extended t in your code. According to the docs this is the index into the vertices defined by the remaining arguments. I don't have MATLAB installed on this machine, but what happens if you do:
allx = [x; x;-x;-x];
ally = [y;-y; y;-y];
allz = [z; z; z; z];
s = size(x,1);
t = [t; t + s; t + 2*s; t + 3*s]
Just trying to think if this makes sense and if/how it extends into the other quadrants.

How can I visualize/plot temperature gradient using matlab?

I have data at specific points in 3D rectangle and I want to see temperature gradient. I have values at specific points , but I want a continous flow of gradient between each sensor. I am not been able to figure out how to visualize or map data in between each sensors placed at different points. stucked :(
X=[5 0 0 0 0 5 10 10 10 10 0 5 10 10 0 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 0 5 10 0 5 10 10 10 5 0 0]';
Y=[10 10 5 5 10 10 5 10 5 10 0 0 0 0 0 0 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 0 0 0 0 0 0 5 10 10 10 5 ]';
Z=[20 20 20 14 14 14 14 14 20 20 20 20 20 14 14 14 3.8 3.8 0 0 7.5 7.5 10 10 12.5 12.5 15 15 17.5 17.5 20 20 0 0 0 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5]';
%# temperature vector
T = [20 22 24 22.1 26.1 22.4 15 17 21 22 19 22 18 17 18 20 21 22 21 24 22.3 22.5 22.8 28.9 22 27 26 20 19 24 21 23 19 18 22 25 27 21 29 25 22 21 22];
scatter3(X,Y,Z,[4000],T,'.');
grid off
box off
view(32,18); axis equal tight off vis3d; % azimuth 26
camproj perspective
camlight; lighting gouraud; alpha(0.75);
rotate3d on
Code below just shows how my one side of 3d rectangle should look like(its just a random code)
datagrid = 500*peaks(100);
R = makerefmat('RasterSize',size(datagrid));
[aspect,slope,gradN,gradE] = gradientm(datagrid,R);
figure; axesm eqacyl
meshm(datagrid,R)
colormap (jet(64))
colorbar('vert')
title('Peaks: elevation')
axis square
You can break the problem down into two sub-problems:
Interpolation
Visualization
Let's take a look at interpolation first. There are many methods available but let's try the MATLAB function griddatan. This will interpolate (linearly) values onto a new set of points (here I've used a regular grid constructed using meshgrid).
M = 20;
N = 20;
L = 40;
T = transpose(T);
% Set up the grid of points we wish to interpolate at
[xi,yi,zi] = meshgrid(linspace(0,10,M),linspace(0,10,N),linspace(0,20,L));
% Perform interpolation
ti = griddatan([X,Y,Z],T,[xi(:),yi(:),zi(:)]);
% Reshape back from a column vector to a MxNxL matrix
ti = reshape(ti, size(xi));
% Visualise in some way
scatter3(xi(:),yi(:),zi(:),400,ti(:),'.')
When it comes to visualization then the sky's the limit and 3D volume visualization is more of an art than a science. I'm afraid I can't run your example (I don't have access to makerefmat) but http://www.mathworks.co.uk/help/techdoc/visualize/bqliccy.html has some good starting points.