Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot two figures in MATLAB

Tags:

plot

matlab

I am implementing a clustering algorithm for n data points and I want to plot n data points in a figure before clustering and in another figure after clustering meaning that there should be two figures in same file with same data points.

My code is like:

X = 500*rand([n,2]);
plot(X(:,1), X(:,2), 'r.')                   1

%Some coding section here

After:

symbs = {'r+','g.','bv','m*','ko'};
hold on
for i = 1: length(I)
    plot(X(C==i,1), X(C==i,2), symbs{i})     2
end

I just want to plot (1) in one figure and (2) in another.

like image 573
user1416605 Avatar asked May 27 '12 16:05

user1416605


People also ask

How to plot two variables on two different figures in MATLAB?

In Matlab, if we plot a variable and after that, we plot another variable, the second variable will overwrite the first variable. To solve this problem, we have to use the figure command. The figure command is used to initialize a figure. For example, if we want to plot two variables on two different figures.

How to plot two graphs on two different figures in Excel?

Let’s plot two graphs on two different figures using the figure command. See the code below. There are two figures, Figure1 and Figure2 in the output, but there will only be one figure with one plot if we don’t use the figure command. You can also give a title name to each figure using the Name property of the figure command.

How to combine multiple plots in one plot?

However, you can use the hold on command to combine multiple plots in the same axes. For example, plot two lines and a scatter plot. Then reset the hold state to off. x = linspace (0,10,50); y1 = sin (x); plot (x,y1) title ( 'Combine Plots' ) hold on y2 = sin (x/2); plot (x,y2) y3 = 2*sin (x); scatter (x,y3) hold off.

How do I create a second figure window instead of subplot?

If all you want is a second figure window, instead of doing subplot you can simply say figure in the place where I put the second call to subplot and a new figure window will be created. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.


1 Answers

Try subplot:

figure;
subplot(1,2,1)
plot(firstdata)
subplot(1,2,2)
plot(seconddata)

This will create two axes areas within the same figure window... from your description, this is my best guess as to what you want.

Edit: From the comments below, here is what you are doing

n=50;
X = 500*rand([n,2]);
subplot(1,2,1); #% <---- add 'subplot' here
plot(X(:,1),X(:,2),'r.')
symbs= {'r+','g.','bv','m*','ko'}; 
subplot(1,2,2); #% <---- add 'subplot' here (with different arguments)
hold on
for i = 1: length(I)
plot(X(C==i,1),X(C==i,2),symbs{i})
end

If all you want is a second figure window, instead of doing subplot you can simply say figure in the place where I put the second call to subplot and a new figure window will be created.

figure; #% <--- creates a figure window
n=50;
X = 500*rand([n,2]);
plot(X(:,1),X(:,2),'r.') #% <--- goes in first window


symbs= {'r+','g.','bv','m*','ko'}; 
figure; #% <---- creates another figure window
hold on
for i = 1: length(I)
plot(X(C==i,1),X(C==i,2),symbs{i}) #% <--- goes in second window
end
like image 100
tmpearce Avatar answered Oct 22 '22 09:10

tmpearce