How to create a contourf plot from a table? - matlab

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)

Related

Editing distance between tick marks

I am currently using subplots in MATLAB and this is my x tick mark data:[.4 .5 .6 .9 1.2 1.5 2 2.5 3 4 5 6 7 8 9 10 12 15 20 30 40]
I am trying to determine if there is a way to space the tick marks evenly, or expand the spacing for the lower values so you can actually read the numbers. In short I would like the physical spacing of the tick marks to be based on some predetermined constant and not the actual numerical values.
You could use logarythmic scale for x-axis - use semilogx instead of plot.(IMO this would be better in your case)
x = [.4 .5 .6 .9 1.2 1.5 2 2.5 3 4 5 6 7 8 9 10 12 15 20 30 40];
y = x/2; % some example data
figure
semilogx(x,y, '.')
set(gca,'xtick', x)
Another option is to change labels on x-ticks setting xticklabel property. Note that you can set custom values in ticks vector.
x = [.4 .5 .6 .9 1.2 1.5 2 2.5 3 4 5 6 7 8 9 10 12 15 20 30 40];
y = x/2; % some example data
ticks = [];
for t = 1:size(x,2)
ticks = [ticks t];
end
figure
plot(ticks, y, '.') % in this example same as 'plot(y)'
set(gca, 'xtick', ticks,'xticklabel', {.4 .5 .6 .9 1.2 1.5 2 2.5 3 4 5 6 7 8 9 10 12 15 20 30 40})

Error in extracting values from bar chart?

d=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
bar_widh=0.2;
figure;
h = bar(d,bar_widh);
for i=1:2:4
for j=1:4
x=d(i,j)
y=d(i+1,j)
figure,plot(x,y);
end
i=i+1;
end
In this code I had plotted a bar chart and I want to extract values from bar chart. (Values are correctly extracted but only one point is plotted; not a line) But i'm getting wrong results.
My aim is to plot a line between
d(1,1) and d(2,1);d(3,1) and d(4,1);
d(1,2) and d(2,2);d(3,2) and d(4,2);
d(1,3) and d(2,3);d(3,3) and d(4,3);
d(1,4) and d(2,4);d(3,4) and d(4,4);
In first figure I need 2 lines(from 1 column); in second figure I need 2 lines(from 2 column); in third figure I need 2 lines(from 3 column) and in fourth figure I need 2 lines(from 4 column).
no.of figures=no.of columns
Version 2
I tried another version
d=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
bar_widh=0.2;
figure;
h = bar(d,bar_widh);
saveas(h,'testfigure.fig');
clear
close all
h=hgload('testfigure.fig');
ch=get(h,'Children');
l=get(ch,'Children');
x=get(l,'Xdata');
y=get(l,'Ydata');
and i'm getting error as
Error using get
Conversion to double from cell is not possible.
Error in Untitled5 (line 10)
l=get(ch,'Children');
d=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16];
bar_widh=0.2;
figure;
h = bar(d,bar_widh);
figure; hold on;
for i = 1:size(d,2)
x = [d(1,i) d(2,i)];
y = [d(3,i) d(4,i)];
plot(x,y);
end
To plot a line, you must make sure the parameters x and y in plot(x,y) are vectors than scalars. For example, to plot a line between P1 = [P1x, P1y] and P2 = [P2x, P2y], the paramenters should be:
x = [P1x, P2x]; y = [P1y, P2y];

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.

How to show only the existing data points on x axis of bar graph in MATLAB?

I need to simple plot B vs. A as bar plot in MATLAB, but I don't want my x axis showing completely from 1 to 274. I only need to show the existing data point on my x axis, which can be done easily in Excel as in the image below. How can MATLAB do this?
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 20 25 27 29 37 40 42 43 48 73 204 242 274];
B=[30 15 5 9 5 6 3 3 2 1 4 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1];
You need to set both 'XTick' and 'XTickLabel' axes properties:
bar(B);
set(gca,'XTickLabel',A)
set(gca,'XTick',1:numel(A));
xlim([0 numel(A)+1]);
Here is an inelegant, but, nonetheless, working solution to your question:
x = [1,4, 6, 7]; % Your data
uni = unique(x)
yMax = length(find(x == mode(x))) + 1;
c = cell(1, length(uni));
c = strread(num2str(uni),'%s')
hist(1:length(uni));
axis([0 length(uni) 0 yMax])
set(gca, 'XTick', 1:length(uni));
set(gca, 'XTickLabel', c);
Basically, this plots the histogram as if the data were spread from 1 to the number of unique elements. Then, it sets the tick marks at each histogram value. Then, it labels each tick mark with the correct number.

How to plot a 3d surface graph in 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'})