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?
Related
I need to identify the 99% probability contour of a GMM fitted to data. Following this example, I'd like to be able to specify which contours to plot, and the x,y, of them.
mu1 = [1 2]; Sigma1 = [2 0; 0 0.5];
mu2 = [-3 -5]; Sigma2 = [1 0;0 1];
X = [mvnrnd(mu1,Sigma1,1000); mvnrnd(mu2,Sigma2,1000)];
GMModel = fitgmdist(X,2);
figure
y = [zeros(1000,1);ones(1000,1)];
h = gscatter(X(:,1),X(:,2),y);
hold on
gmPDF = #(x,y) arrayfun(#(x0,y0) pdf(GMModel,[x0 y0]),x,y);
g = gca;
fcontour(gmPDF,[g.XLim g.YLim])
title('{\bf Scatter Plot and Fitted Gaussian Mixture Contours}')
legend(h,'Model 0','Model1')
hold off
So, in the following figure, I'd like to to be able to plot the 99% in dashed black line "k". Any idea how to accomplish this?
You can display and get the coordinates of the given contour line specifying the LevelList property of fcontour, and then reading the ContourMatrix property of the contour handle:
% Random function, insert here yours
f = #(x,y) arrayfun(#(x0,y0) x0.^2 + y0.^2 - 0.1,x,y);
% The function value you want to get the contour for
lvl = 0.99;
% Plot the contour line
cHandle = fcontour(f, '--k', 'LevelList', [lvl]);
hold on
% Get the coordinates
lvlX = cHandle.ContourMatrix(1, 2:end);
lvlY = cHandle.ContourMatrix(2, 2:end);
% For a check:
plot(lvlX, lvlY, '--r')
I have the following script:
close all; clear all; clc;
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
values = -10:0.5:10;
figure
[C,hh] = contour(X, Y, Z, values,'r', 'LineWidth',1);
clabel(C, hh, values, 'fontsize',7)
As you can see in the contour lines, all of the lines are plotted with LineWidth = 1. I would like to plot special line for the value = 0, with LineWidth = 2, how to set it? Thanks a lor for your help.
You will need to make a secondary contour plot to highlight the desired contour levels. The MathWorks has an example of this in the documentation.
For your case we'll have something like the following:
% Generate sample data
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
values = -10:0.5:10;
% Generate initial contour plot
figure
[C,hh] = contour(X, Y, Z, values,'r', 'LineWidth',1);
clabel(C, hh, values, 'fontsize',7)
% Generate second contour plot with desired contour level highlighted
hold on
contour(X, Y, Z, [0 0], 'b', 'LineWidth', 2);
hold off
Which returns the following:
Not that I've specified the single contour level as a vector. This is explained by the documentation for contour:
contour(Z,v) draws a contour plot of matrix Z with contour lines at the data values specified in the monotonically increasing vector v. To display a single contour line at a particular value, define v as a two-element vector with both elements equal to the desired contour level. For example, to draw contour lines at level k, use contour(Z,[k k])
If you want to highlight multiple levels then this does not apply (e.g. contour(X, Y, Z, [-1 0], 'b', 'LineWidth', 2) to highlight -1 and 0)
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!
How can I make the vertical axes of two plots equal?
For example:
a = [1 2 3; 21 1 3; 4 2 3; 4 5 6]
After plotting plot(a(1, :)) I get the following figure:
I have done some simple operations:
[U E V] = svd(a);
figure(2);
plot(U(1,:))
And get another figure:
How do I make the y-axis limits of both plots equal? Is it with the axes equal command?
UPDATE:
I've used the following commands:
figure (1)
ylim([0 3])
plot(a(1,:))
figure (2);
ylim([0 3])
plot(U(1,:))
But get the same result...
You can use ylim to force limits on the y-axis. For example:
figure(1)
%// Some plotting...
ylim([0 3])
figure(2)
%// Some more plotting
ylim([0 3])
This ensures that the y-axis is limited to the range [0, 3] in both plots. You can do the same for the limits of the x-axis with the command xlim.
Also note that if you want to set the limits for both axes at once, instead of using xlim and ylim (two commands), you can use axis (one command).
you can use the ylim or xlim functions.
You can clone the limits of one plot to another plot in this fashion:
h1 = figure;
% do first plot...
h2 = figure;
%do second plot...
% set first figure as active
figure(h1);
%get limits properties of the axes that are drawn in Figure 1
xL = get(gca, 'XLim');
yL = get(gca, 'YLim');
%switch to second figure and set it as active
figure(h2);
%set axis limit properties of Figure 2 to be the same as in Figure 1
set(gca, 'XLim', xL);
set(gca, 'YLim', yL);
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')