MATLAB Answers

## How do I combine multiple plots in one graph?

Asked by Tom

### Tom (view profile)

on 10 Dec 2011
Latest activity Commented on by Ina Panci

### Ina Panci (view profile)

on 10 May 2019 at 21:09
Accepted Answer by Chandra Kurniawan

### Chandra Kurniawan (view profile)

Hi, is there somewhere I can see some examples of scripts that result in multiple plots on one graph?
I know the 'hold' function is part of how to do it, but I'm very new to Matlab and I really need to see some full examples.
If someone could point me towards some that'd be great.

tayyaba khurshid

### tayyaba khurshid (view profile)

on 4 Mar 2017
snr=0:1:15;
for ii=1:length(snr)
x=randint(1,100000); % Generating bits
y=pskmod(x,2); % Generating BPSK modulated data
ynoisy=awgn(y,snr(ii)); % Adding AWGN noise to data
z=pskdemod(ynoisy,2); % Demodulating
error(ii)=length(find(x-z~=0));
end
semilogy(snr,error)
hold
snr=0:1:15;
for ii=1:length(snr)
x=randint(1,100000); % Generating bits
y=pskmod(x,4); % Generating QPSK modulated data
ynoisy=awgn(y,snr(ii)); % Adding AWGN noise to data
z=pskdemod(ynoisy,4); % Demodulating
error(ii)=length(find(x-z~=0));
end
semilogy(snr,error)
grid
Chiemela Victor Amaechi

### Chiemela Victor Amaechi (view profile)

on 26 Jul 2018
You can use this code, but to have multiple plots, you need the subplot which is used as follows:
% Code used in my paper on design of composite riser
% Create a vertical bar chart using the bar function
figure
subplot(2,3,1) % add fourth plot in 2 x 3 grid
bar(1:18, [FibreControl' FibreAxial'], 1)
% Add title and axis labels
title('(a)')
xlabel('Composite Riser Layers')
ylabel('Stress in Fibre Direction (MPa)')
% Add a legend
legend('[1.58:1.62:1.86]', '[1.84:1.62:1.86]')
% Set the axis limits
set(gca, 'XTick', 1:18)
set(gca, 'XLim', [0,19])
set(gca, 'Fontsize', 8)
subplot(2,3,2) % add first plot in 2 x 3 grid
bar(1:18, [FibreControl' FibreAngled'], 1)
% Add title and axis labels
title('(b)')
xlabel('Composite Riser Layers')
ylabel('Stress in Fibre Direction (MPa)')
% Add a legend
legend('[1.58:1.62:1.86]', '[1.58:1.88:1.86]')
% Set the axis limits
set(gca, 'XTick', 1:18)
set(gca, 'XLim', [0,19])
set(gca, 'fontname','arial','fontsize',8,'linewidth',1)
subplot(2,3,3) % add second plot in 2 x 3 grid
bar(1:18, [FibreControl' FibreHoop'], 1)
% Add title and axis labels
title('(c)')
xlabel('Composite Riser Layers')
ylabel('Stress in Fibre Direction (MPa)')
% Add a legend
legend('[1.58:1.62:1.86]', '[1.58:1.62:1.60]')
% Set the axis limits
set(gca, 'XTick', 1:18)
set(gca, 'XLim', [0,19])
set(gca, 'Fontsize', 8)

Sign in to comment.

## 6 Answers

### Chandra Kurniawan (view profile)

Answer by Chandra Kurniawan

### Chandra Kurniawan (view profile)

on 10 Dec 2011
Edited by MathWorks Support Team

### MathWorks Support Team (view profile)

on 9 Nov 2018
Accepted Answer

To combine multiple plots in one graph, use the “hold on” command, such as:
plot(1:10)
hold on
plot(11:20)
hold off
For more information, see:

Walter Roberson

### Walter Roberson (view profile)

on 10 Dec 2011
The alternative to "hold on" that would be applicable to this example is
plot(t,x,'r',t,y,'b')
Ina Panci

### Ina Panci (view profile)

on 10 May 2019 at 21:09
Hello, what other letters can we use if we have more than two functions to plot?

Sign in to comment.

### N Narayan rao (view profile)

Answer by N Narayan rao

### N Narayan rao (view profile)

on 25 Sep 2016
Edited by Walter Roberson

### Walter Roberson (view profile)

on 15 Jul 2017

x = -3:0.1:3;
y1 = sin(x);
y2 = cos(x);
y3= tan(x);
y4=1./cos(x);
figure
subplot(2,2,1)
plot(x,y1)
title('Subplot 1')
subplot(2,2,2)
plot(x,y2)
title('Subplot 2')
subplot(2,2,3)
plot(x,y3)
title('Subplot 3')
subplot(2,2,4)
plot(x,y4)
title('Subplot 4')

#### 0 Comments

Sign in to comment.

Answer by Tom

### Tom (view profile)

on 10 Dec 2011

Thanks, that's perfect.

#### 0 Comments

Sign in to comment.

### Raghuram (view profile)

Answer by Raghuram

### Raghuram (view profile)

on 27 Mar 2014

Could you please tell me the same with scatter plot command? That is, to plot multiple scatter plots in one figure?

Prasad Kalane

### Prasad Kalane (view profile)

on 11 Dec 2015
x=rand(2,10);
scatter(x(1,:),x(2,:),'r');
hold on
x=rand(2,10);
scatter(x(1,:),x(2,:),'g');
hold on
x=rand(2,10);
scatter(x(1,:),x(2,:),'b');
hold on
x=rand(2,10);
scatter(x(1,:),x(2,:),'c');
You can plot multiple plot just use "hold on" command
MOHAMMAD YASIR ALI

### MOHAMMAD YASIR ALI (view profile)

on 13 Jul 2017
can I get best fit line from this method?
Walter Roberson

### Walter Roberson (view profile)

on 15 Jul 2017
"can I get best fit line from this method?"
Best fit lines tend to imply an ordering to the data, but scatter plots tend to imply the data might not be ordered in a way that is compatible with fitting.
But yes, if you have polynomial coefficients for your fit, such as coefficients generated using polyfit(), then you can use polyval() to interpolate values, and you can "hold on" and plot() the values.

Sign in to comment.

### Cortney Marschner (view profile)

Answer by Cortney Marschner

### Cortney Marschner (view profile)

on 8 Oct 2016
Edited by Walter Roberson

### Walter Roberson (view profile)

on 15 Jul 2017

How do I plot multiple graphs on the same graph using this code?:
hold off
[x, y] = meshgrid(0:0.2:10, 0:0.2:6);
X = x(1,1:51);
dx = ones(size(x));
dy = x;
quiver(x, y, dx, dy)
axis tight
hold on
plot(X, 1+X.^2/2)

Walter Roberson

### Walter Roberson (view profile)

on 15 Jul 2017
That code already plots multiple lines on the same plot. However, the y range of the second one is much larger than the y range of the first, so the second one takes up most of the space.

Sign in to comment.

### utkarsh singhvi (view profile)

Answer by utkarsh singhvi

### utkarsh singhvi (view profile)

on 17 Apr 2018

command to plot multiple function on one graph

#### 0 Comments

Sign in to comment.