How to plot multiple graphs in one figure ? (2024)

2,412 views (last 30 days)

Show older comments

sree chandana munnangi on 29 Jun 2021

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure

Commented: Stefan Bendisch on 18 Aug 2023

Accepted Answer: Adam Danz

  • deec.txt
  • modleach.txt

I have two codes. Each code has four graphs. I want to plot two graphs in one figure. For example: Dead nodes vs Round graph of two should be in one figure. In the same way other graphs also. I tried hold on function but still not getting. How to merge the two codes in order to get the graphs ?

3 Comments

Show 1 older commentHide 1 older comment

Amit Bhowmick on 29 Jun 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure#comment_1610063

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure#comment_1610063

Hi sree,

The question is not clear. you can check with following modifications:

Mod: 1

figure (1);

hold

plot(r,STATISTICS.DEAD);

xlabel('Rounds');

ylabel('Dead Nodes');

title('MODLEACH');

%figure (2);

plot(r,STATISTICS.PACKETS_TO_BS);

xlabel('Rounds');

ylabel('Packets to BS');

title('MODLEACH');

%figure (4);

plot(r,STATISTICS.PACKETS_TO_CH);

xlabel('Rounds');

ylabel('Packets to CH')

title('MODLEACH');

%figure (5);

plot(r,STATISTICS.ALLIVE);

xlabel('Rounds');

ylabel('Allive nodes')

title('MODLEACH');

Mod: 2

If you are not getting two curve in a single figure using hold command then try to use it,

if(ishold==0)

hold on;

end

Amanda Liu on 29 Jun 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure#comment_1610078

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure#comment_1610078

Edited: Amanda Liu on 29 Jun 2021

There are other ways but I'm using subplot.

How to plot multiple graphs in one figure ? (4)

figure(1)

subplot(2,1,1) % 2 rows, 1 column, first position

plot(r,STATISTICS.DEAD);

title('Dead Nodes vs Rounds')

xlabel 'Rounds';

ylabel 'Dead Nodes';

subplot(2,1,2) % 2 rows, 1 column, second position

plot(r,STATISTICS.ALLIVE);

title('Live Nodes vs Rounds')

xlabel 'Rounds';

ylabel 'Live Nodes';

figure(2)

subplot(2,1,1)

plot(r,STATISTICS.PACKETS_TO_BS);

title('Pkts to BS per round')

xlabel 'Rounds';

ylabel 'Pkts to BS ';

subplot(2,1,2)

plot(r,STATISTICS.PACKETS_TO_CH);

title('Pkts to CH per round')

xlabel 'Rounds';

ylabel 'Pkts to CH ';

Nghia Tran on 2 Feb 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure#comment_2595501

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure#comment_2595501

thank u

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Adam Danz on 20 May 2023

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure#answer_1239879

> I want to plot two graphs in one figure

Options are

  • subplot
  • tiledlayout with nexttile - preferred, starting in R2019b

subplot(m,n,i) creates an axes in the i^th position of an m-by-n grid.

tiledlayout(m,n) creates an m-by-n grid upon which axes can be added using nexttile.

See documentation links for details.

Benefits to using tiledlayout

The following features are great improvements available in tiledlayout.

  1. Flexible grid sizes (TileArrangment property)
  2. Control spacing (TileSpacing and Padding properties, Community Highlight)
  3. Row-wise or column-wise order of axes (TileIndexing property, Community Highlight)
  4. Global labels such as a title, subtitle, xlabel and ylabel (labels properties)
  5. Global legends, starting in R2020b (legend layout property, documentation example, more demos)
  6. Global colorbars, starting in R2020b (colorbar layout property, documentation example, demo)
  7. Specify the span of an axes within the grid using nexttile(span)

Demo

tcl = tiledlayout(2,3,'TileSpacing','compact');

nexttile

plot(magic(5))

axis tight

nexttile

scatter(rand(1,10), rand(1,10), 90, lines(10))

box on

nexttile([2,1])

imagesc(peaks(200))

nexttile([1,2])

histogram2(2*randn(1,1000), randn(1,1000), 'FaceColor', 'Flat')

cb = colorbar();

cb.Layout.Tile = 'south';

title(tcl, 'Global title')

xlabel(tcl, 'Global xlabel')

ylabel(tcl, 'Global ylabel')

How to plot multiple graphs in one figure ? (7)

1 Comment

Show -1 older commentsHide -1 older comments

Stefan Bendisch on 18 Aug 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure#comment_2853082

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure#comment_2853082

Thank you. This function is a great help since it overcomes come limitations of subplot.

Sign in to comment.

More Answers (1)

Devanuj Deka on 12 Jul 2021

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure#answer_744738

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/867663-how-to-plot-multiple-graphs-in-one-figure#answer_744738

It is my understanding that you want to plot two graphs in one figure. You tried hold on but it didn't work.

It is not clear whether you want both plots in the same graph, or both plots in separate graphs but in the same window. Below are the possible solutions for either of those which you can try. I've taken dead nodes v/s rounds and alive nodes v/s rounds for the plots.

1) hold on, hold off – both dead and alive nodes in the same plot, same figure

figure(1)

plot(r,STATISTICS.DEAD);

hold on;

title('Nodes vs Rounds')

plot(r,STATISTICS.ALLIVE);

xlabel 'Rounds';

ylabel 'Nodes';

hold off;

legend('Dead Nodes','Live Nodes','Location','best');

Documentation: hold

2) subplot – dead nodes and alive nodes in two separate plots, but in the same figure

figure(1)

subplot(2,1,1)

plot(r,STATISTICS.DEAD);

title('Dead Nodes vs Rounds')

xlabel 'Rounds';

ylabel 'Dead Nodes';

subplot(2,1,2)

plot(r,STATISTICS.ALLIVE);

title('Live Nodes vs Rounds')

xlabel 'Rounds';

ylabel 'Live Nodes';

Documentation: subplot

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D Plots

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

Tags

  • subplot
  • tiledlayout
  • figure
  • multiple
  • axes

Products

  • MATLAB

Release

R2021a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to plot multiple graphs in one figure ? (10)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to plot multiple graphs in one figure ? (2024)
Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5977

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.