Plotting array of x and y values as points - matlab

I can't understand this: when I write the following code I obtain this graph in matlab.
x = [0 1 2 3 4 5];
y = [0 1 0 1 0 1];
figure
plot(x,y);
I had just expected that only the points written in arrays x and y would be plotted ,but the graph shows lines also...
I can't understand why is it so... Please help where am I wrong

Try to use the following
figure(10);
plot(x,y, '.');
figure(20);
plot(x,y, 'x');
figure(30);
plot(x,y, '-r');
See the differences... a dot-scatter, x-scatter and red line plot.
In the plot documentation you can read more about line styles. By default it is a blue line, as you can see in your plot!

Related

matlab contour plot specific value

I have made a contour plot in matlab (See code). And I want to find the contour line where the value is equal to 1. Now I just have found it approximately between to lines contour plot:
Can this be done? For example if I want to plot 5 contour lines from the values 0 to 1
Update I managed to plot contour line equal to 1, but I want the contour lines inside, not outside the contour line =1 as I get with this code.
[x,y] = meshgrid(-3 : 0.01: 3, -3 : 0.01: 3);
s = x + i*y;
z=abs(1+s+((s.^2)/2)+((s.^3)/6));
figure;
[C,h] = contour(x,y,z,[1 1]);
clabel(C,h)
hold on;
[R,k] = contour(x,y,z,25);
clabel(R,k)
How about:
[C,h] = contour(x,y,z,0.1:0.1:1);
clabel(C,h)
% no need for 'hold on' and all the rest...
That's what you look for?

Fill area between a polyline and a horizontal line in Matlab/Octave

I'm trying to fill the areas between a polyline and a horizontal line (that cut this polyline in several points) using Octave/Matlab, but I want to keep only the areas below the horizontal line.
This is what I've come up so far:
x = 0:0.5:6;
y = [3 2.5 1 1 1 2.5 3 2.5 1 1 1 2.5 3];
yline(1:13) = 2;
figure(1)
plot(x,y,'k')
fill([x fliplr(x)],[y yline],'g')
axis equal
xlim([-1 7]);
I searched for days to find a solution but I only went close to the answer here, here and here (unfortunately this last one is only for r-code).
You can use the following trick:
Fill normally, as you do in your code. No need for coloring the edges; it will be done later.
Draw a white rectangular patch to cover the part you don't want filled. No edge color here either.
Plot the lines on top of that.
Code:
x = 0:0.5:6;
y = [3 2.5 1 1 1 2.5 3 2.5 1 1 1 2.5 3];
yline(1:13) = 2;
figure(1)
fill([x fliplr(x)],[y yline],'g', 'edgecolor', 'none')
hold on
patch([min(x) max(x) max(x) min(x)],[yline(1) yline(1) max(y) max(y)], 'w', ...
'edgecolor', 'none')
plot(x,y,'k')
plot(x,yline,'k')
axis equal
xlim([-1 7]);
Resulting figure:

Draw a portion of a line in Matlab

I just wanna draw a portion of a line. I'll try to be more specific.
Now I wanna see just a portion of the red line..but I don't know how to do it in Matlab. I wanna show like a little portion around the red dot.
Use plot command:
x = [0 3];
y = [1 -4];
plot(x,y,'r');
In order to draw 2 shapes together, use hold on:
x = -2*pi:0.01:2*pi;
y = sin(x);
plot(x,y,'k');
hold on;
x = [0 3];
y = [1 -4];
plot(x,y,'r');
use line to draw what you need. Say you want to draw a point going from (0,1) to (3,-4), type:
line ([0 3], [1 -4])

Data label on each entry in xy scatter

I have an x-y scatter plot in MATLAB and want to put a data label on each point. I can't seem to find this in the documentation. Is it possible?
Example:
p = rand(10,2);
scatter(p(:,1), p(:,2), 'filled')
axis([0 1 0 1])
labels = num2str((1:size(p,1))','%d'); %'
text(p(:,1), p(:,2), labels, 'horizontal','left', 'vertical','bottom')

Matlab question: fixing the scale of Y axis in hist

I want to draw several histograms. But I want the Y axis to be fixed, like from 1000 to 1 by 100. How can I specify them。
Please advise.
Consider this example:
%# some data
X = randn(1000,3);
nbins = 10;
%# compute frequencies and bins
%#[count,bins] = hist(X, nbins);
count = zeros(10,size(X,2));
bins = zeros(10,size(X,2));
for i=1:size(X,2)
[count(:,i),bins(:,i)] = hist(X(:,i),nbins);
end
%# show histograms
for i=1:size(X,2)
subplot(1,size(X,2),i)
bar(bins(:,i), count(:,i),'hist')
set(gca, 'YTick',0:100:4000, 'YLim',[0 400])
end
The axis command is what you're looking for. You specify the [XMIN XMAX YMIN YMAX]. This example will have all histograms capped at a value of 5. Also, you're asking a bunch of questions about MATLAB today without seemingly doing any research. Please ask a search engine and show that you've at least tried something.
clf;
subplot(1,2,1); hist(rand(1,10)); axis([0 1 0 5]);
subplot(1,2,2); hist(rand(1,10)); axis([0 1 0 5]);