multiple graphs in one script
100 views (last 30 days)
Show older comments
Hey all,
I would like to plot several graphs in one script. However, it keeps giving me the last graph one. please see below code
%% 6
x = ([-2*pi:0.1:2*pi]);
f = x-sin(x)
g = 1-x.*cos(x)
plot(x,f,x,g)
title('f(x) & g(x)')
subplot(1,2,1)
plot(x,f)
title('f(x)')
subplot(1,2,2)
plot(x,g)
title('g(x)')
what is the wrong here
0 Comments
Answers (2)
Wan Ji
on 5 Sep 2021
Modified the code as
%% 6
x = ([-2*pi:0.1:2*pi]);
f = x-sin(x)
g = 1-x.*cos(x)
figure(1);clf;
plot(x,f,x,g)
title('f(x) & g(x)')
figure(2);clf;
subplot(1,2,1)
plot(x,f)
title('f(x)')
subplot(1,2,2)
plot(x,g)
title('g(x)')
0 Comments
Robert U
on 6 Sep 2021
Hi basim almutairi,
Simplify your life and use handles (start here: graphics-objects): figure handles, axes handles, ...
Handles allow to modify properties even later in code. It makes it easier to debug and to modify plots.
x = (-2*pi:0.1:2*pi);
f = x-sin(x);
g = 1-x.*cos(x);
fh{1} = figure(1);
ah{1} = axes(fh{1});
plot(ah{1},x,f,x,g)
title(ah{1},'f(x) & g(x)')
fh{2} = figure(2);
sh{1} = subplot(1,2,1,'Parent',fh{2});
plot(sh{1},x,f)
title(sh{1},'f(x)')
sh{2} = subplot(1,2,2,'Parent',fh{2});
plot(sh{2},x,g)
title(sh{2},'g(x)')
Kind regards,
Robert
0 Comments
See Also
Categories
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!