Azzera filtri
Azzera filtri

How do I combine multiple plots in one graph?

423 visualizzazioni (ultimi 30 giorni)
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 Commenti
Chiemela Victor Amaechi
Chiemela Victor Amaechi il 26 Lug 2018
Modificato: madhan ravi il 19 Nov 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)
Mebin Cherian
Mebin Cherian il 25 Nov 2020
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?
semilogy(snr,error(i));
Label=append('Error',error(i));
label(Label);
hold on;

Accedi per commentare.

Risposta accettata

Chandra Kurniawan
Chandra Kurniawan il 10 Dic 2011
Modificato: MathWorks Support Team il 9 Nov 2018
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:
  3 Commenti
Ina Panci
Ina Panci il 10 Mag 2019
Hello, what other letters can we use if we have more than two functions to plot?
Cleat Zeiler
Cleat Zeiler il 15 Ott 2019
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

Accedi per commentare.

Più risposte (7)

N Narayan rao
N Narayan rao il 25 Set 2016
Modificato: Walter Roberson il 15 Lug 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')
  1 Commento
AT
AT il 31 Dic 2021
How can we run many simulations at the same time? (for example for 1000 simulations)
or
How can we plot more than 100 graphs in one plot?

Accedi per commentare.


Tom
Tom il 10 Dic 2011
Thanks, that's perfect.

Raghuram
Raghuram il 27 Mar 2014
Could you please tell me the same with scatter plot command? That is, to plot multiple scatter plots in one figure?
  3 Commenti
MOHAMMAD YASIR ALI
MOHAMMAD YASIR ALI il 13 Lug 2017
can I get best fit line from this method?
Walter Roberson
Walter Roberson il 15 Lug 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.

Accedi per commentare.


Cortney Marschner
Cortney Marschner il 8 Ott 2016
Modificato: Walter Roberson il 15 Lug 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)
  1 Commento
Walter Roberson
Walter Roberson il 15 Lug 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.

Accedi per commentare.


utkarsh singhvi
utkarsh singhvi il 17 Apr 2018
command to plot multiple function on one graph

Emil Latypov
Emil Latypov il 16 Apr 2020
Modificato: Image Analyst il 16 Apr 2020
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.
clear
clc
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,:));
end
mysol
mysol.acidity
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 ';
else
acidity(i,:) = 'neutral';
end
end
end
  2 Commenti
Image Analyst
Image Analyst il 16 Apr 2020
How is this an answer to Tom's question on "multiple plots on one graph"? I think you need to start your own question rather than post an answer to Tom's question.
Walter Roberson
Walter Roberson il 17 Apr 2020
The line
mysol(i).acidity = strcat(acidity(i,:));
should probably be rewritten in terms of strtrim()

Accedi per commentare.


Sankirna D. Joge
Sankirna D. Joge il 17 Ago 2020
One must use hold on command after every plot,
plot(x1,y1);
hold on
plot(x2,y2);
hold on
plot(x3,y3);
hold off
This will give output as three plots on a same graph.
  1 Commento
Walter Roberson
Walter Roberson il 17 Ago 2020
This is not correct. "hold on" stays in effect until you turn it off (or the axes is cleared or deleted.)

Accedi per commentare.

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by