How to plot multiple graphs in one figure ?

2,453 ビュー (過去 30 日間)
sree chandana munnangi
sree chandana munnangi 2021 年 6 月 29 日
コメント済み: Stefan Bendisch 2023 年 8 月 18 日
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 件のコメント
Amanda Liu
Amanda Liu 2021 年 6 月 29 日
編集済み: Amanda Liu 2021 年 6 月 29 日
There are other ways but I'm using subplot.
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
Nghia Tran 2023 年 2 月 2 日
thank u

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2023 年 5 月 20 日
> I want to plot two graphs in one figure
Options are
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')
  1 件のコメント
Stefan Bendisch
Stefan Bendisch 2023 年 8 月 18 日
Thank you. This function is a great help since it overcomes come limitations of subplot.

サインインしてコメントする。

その他の回答 (1 件)

Devanuj Deka
Devanuj Deka 2021 年 7 月 12 日
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

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by