How do I combine multiple plots in one graph?

조회 수: 1,406(최근 30일)
Tom 2011년 12월 10일
댓글: AT 2021년 12월 31일
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.
  댓글 수: 3
Mebin Cherian
Mebin Cherian 2020년 11월 25일
Hey, @tayyaba, I have to plot graphs of various iterations into the same graph, and the labels are written during each iteration. My problem is that the legend gets overwritten each time instead of being appended to the already existing legend. soln?
hold on;

댓글을 달려면 로그인하십시오.

채택된 답변

Chandra Kurniawan
Chandra Kurniawan 2011년 12월 10일
편집: MathWorks Support Team 2018년 11월 9일
To combine multiple plots in one graph, use the “hold on” command, such as:
hold on
hold off
For more information, see:
  댓글 수: 3
Cleat Zeiler
Cleat Zeiler 2019년 10월 15일
This is the list from the help call:
Various line types, plot symbols and colors may be obtained with
plot(X,Y,S) where S is a character string made from one element
from any or all the following 3 columns:
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram

댓글을 달려면 로그인하십시오.

추가 답변(7개)

N Narayan rao
N Narayan rao 2016년 9월 25일
편집: Walter Roberson 2017년 7월 15일
x = -3:0.1:3;
y1 = sin(x);
y2 = cos(x);
y3= tan(x);
title('Subplot 1')
title('Subplot 2')
title('Subplot 3')
title('Subplot 4')
  댓글 수: 1
AT 2021년 12월 31일
How can we run many simulations at the same time? (for example for 1000 simulations)
How can we plot more than 100 graphs in one plot?

댓글을 달려면 로그인하십시오.

Tom 2011년 12월 10일
Thanks, that's perfect.

Raghuram 2014년 3월 27일
Could you please tell me the same with scatter plot command? That is, to plot multiple scatter plots in one figure?
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 7월 15일
"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.

댓글을 달려면 로그인하십시오.

Cortney Marschner
Cortney Marschner 2016년 10월 8일
편집: Walter Roberson 2017년 7월 15일
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)
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 7월 15일
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.

댓글을 달려면 로그인하십시오.

utkarsh singhvi
utkarsh singhvi 2018년 4월 17일
command to plot multiple function on one graph

Emil Latypov
Emil Latypov 2020년 4월 16일
편집: Image Analyst 2020년 4월 16일
In chemistry, the pH of an aqueous solution is a measure of its acidity. The pH scale ranges from 0 to 14, inclusive. A solution with a pH of 7 is said to be neutral, a solution with a pH greater than 7 is basic, and a solution with a pH less than 7 is acidic.
I did it, but my prof said it is not professional. How to do it professionally?
%% Chapter 8 Question 19
%% This script cteats the a vector structure mysol with various solutions
%% and their pH-values and adds another field mysol.acidity.
format compact
mysol(3) = struct('name','bleach','pH',12);
mysol(1) = struct('name','juce','pH',5);
mysol(2) = struct('name','milk','pH',7);
acidity = myacidity(mysol);
for i = 1:3
mysol(i).acidity = strcat(acidity(i,:));
function acidity = myacidity(mysol)
% This function receives a vector structure with various solutions and
% their pH-values and determines acidity of solutions.
len = length(mysol);
acidity(len,:) = blanks(7);
for i = 1:len
if mysol(i).pH < 7
acidity(i,:) = 'acidic ';
elseif mysol(i).pH > 7
acidity(i,:) = 'basic ';
acidity(i,:) = 'neutral';
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 4월 17일
The line
mysol(i).acidity = strcat(acidity(i,:));
should probably be rewritten in terms of strtrim()

댓글을 달려면 로그인하십시오.

Sankirna D. Joge
Sankirna D. Joge 2020년 8월 17일
One must use hold on command after every plot,
hold on
hold on
hold off
This will give output as three plots on a same graph.
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 8월 17일
This is not correct. "hold on" stays in effect until you turn it off (or the axes is cleared or deleted.)

댓글을 달려면 로그인하십시오.


Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by