How to plot two figures side by side in a same plot?
이전 댓글 표시
I am using subplot command to plot two figures side by side in a same plot but both figures are not appearing. Please help me to detect error in my code. Below is my code.
clear all; close all; clc;
data= [0.1,3.8;
0.1,2.7;
0.01,2.3;
%0.1,2.7;
0.01,0.7;
0.1,3.3;
0.07,4.7;
0.02,0;
0.01,1;
%0.07,4.7;
0.23,3.4;
%0.07,4.7;
%0.07,4.7;
0.1,5.8;
%0.07,4.7;
0.01,4.7;
0.1,0.5;
%0.1,5.8;
0.23,3.1;
0.01,5.9;
0.02,4.2;
0.1,5.9;
0.07,1.5];
subplot (1,2,1)
x = data(:,1);
y = data(:,2);
dy=0.2;
%f=figure;
t=tiledlayout("flow");
nexttile(t);
h=scatter(x,y,100,'filled','MarkerEdgeColor','k');
C = jet(numel(x));
h.CData = C;
set(gca, 'colormap', C)
colorbar()
%xlabel('${\it} (A^{2}/B)^{1/4}$','Interpreter','Latex')
xlabel('${\omega }$','Interpreter','Latex')
ylabel('${\varphi }$','Interpreter','Latex')
labels=["d1","d2,d4","d3","d5","d6","d7,p1,p3,p4,v2","d8","d9","p2","v1,v5","v3","v4","v6","v7","v8","v9","v10"];
text(x,y+dy,labels,"HorizontalAlignment","left","VerticalAlignment","cap",'FontSize',10);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data= [0.1,3.8;
0.1,2.7;
0.01,2.3;
%0.1,2.7;
0.01,0.7;
0.1,3.3;
0.07,4.7;
0.02,0;
0.01,1;
%0.07,4.7;
0.23,3.4;
%0.07,4.7;
%0.07,4.7;
0.1,5.8;
%0.07,4.7;
0.01,4.7;
0.1,0.5;
%0.1,5.8;
0.23,3.1;
0.01,5.9;
0.02,4.2;
0.1,5.9;
0.07,1.5];
subplot (1,2,2)
x = data(:,1);
y = data(:,2);
dy=0.2;
%f=figure;
t=tiledlayout("flow");
nexttile(t);
h=scatter(x,y,100,'filled','MarkerEdgeColor','k');
C = jet(numel(x));
h.CData = C;
set(gca, 'colormap', C)
colorbar()
%xlabel('${\it} (A^{2}/B)^{1/4}$','Interpreter','Latex')
xlabel('${\omega }$','Interpreter','Latex')
ylabel('${\varphi }$','Interpreter','Latex')
labels=["d1","d2,d4","d3","d5","d6","d7,p1,p3,p4,v2","d8","d9","p2","v1,v5","v3","v4","v6","v7","v8","v9","v10"];
text(x,y+dy,labels,"HorizontalAlignment","left","VerticalAlignment","cap",'FontSize',10);
댓글 수: 1
Dyuman Joshi
2024년 1월 25일
Use either of subplot() or tiledlayout().
Not both of them together.
채택된 답변
추가 답변 (2개)
tiledlayout and subplot do not work together. They each offer a means of adding multiple axes to a figure. Tiledlayout is the more modern approach and is recommended over subplot.
The code below makes the following changes from your original code
- Removes subplot
- Replaces tiledlayout('flow') with tiledlayout(1,2) assuming you want a 1x2 grid of axes.
See the 5 changes I made, indicated by arrows in the comments.
clear all; close all; clc;
data= [0.1,3.8;
0.1,2.7;
0.01,2.3;
%0.1,2.7;
0.01,0.7;
0.1,3.3;
0.07,4.7;
0.02,0;
0.01,1;
%0.07,4.7;
0.23,3.4;
%0.07,4.7;
%0.07,4.7;
0.1,5.8;
%0.07,4.7;
0.01,4.7;
0.1,0.5;
%0.1,5.8;
0.23,3.1;
0.01,5.9;
0.02,4.2;
0.1,5.9;
0.07,1.5];
% subplot (1,2,1) % <--------- remove this
x = data(:,1);
y = data(:,2);
dy=0.2;
%f=figure;
% t=tiledlayout("flow"); % <--------- remove this
t = tiledlayout(1,2); % <--------- add this
nexttile(t);
h=scatter(x,y,100,'filled','MarkerEdgeColor','k');
C = jet(numel(x));
h.CData = C;
set(gca, 'colormap', C)
colorbar()
%xlabel('${\it} (A^{2}/B)^{1/4}$','Interpreter','Latex')
xlabel('${\omega }$','Interpreter','Latex')
ylabel('${\varphi }$','Interpreter','Latex')
labels=["d1","d2,d4","d3","d5","d6","d7,p1,p3,p4,v2","d8","d9","p2","v1,v5","v3","v4","v6","v7","v8","v9","v10"];
text(x,y+dy,labels,"HorizontalAlignment","left","VerticalAlignment","cap",'FontSize',10);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data= [0.1,3.8;
0.1,2.7;
0.01,2.3;
%0.1,2.7;
0.01,0.7;
0.1,3.3;
0.07,4.7;
0.02,0;
0.01,1;
%0.07,4.7;
0.23,3.4;
%0.07,4.7;
%0.07,4.7;
0.1,5.8;
%0.07,4.7;
0.01,4.7;
0.1,0.5;
%0.1,5.8;
0.23,3.1;
0.01,5.9;
0.02,4.2;
0.1,5.9;
0.07,1.5];
% subplot (1,2,2) % <-------------- remove this
x = data(:,1);
y = data(:,2);
dy=0.2;
%f=figure;
% t=tiledlayout("flow"); <-------------- remove this
nexttile(t);
h=scatter(x,y,100,'filled','MarkerEdgeColor','k');
C = jet(numel(x));
h.CData = C;
set(gca, 'colormap', C)
colorbar()
%xlabel('${\it} (A^{2}/B)^{1/4}$','Interpreter','Latex')
xlabel('${\omega }$','Interpreter','Latex')
ylabel('${\varphi }$','Interpreter','Latex')
labels=["d1","d2,d4","d3","d5","d6","d7,p1,p3,p4,v2","d8","d9","p2","v1,v5","v3","v4","v6","v7","v8","v9","v10"];
text(x,y+dy,labels,"HorizontalAlignment","left","VerticalAlignment","cap",'FontSize',10);
The subplot and tiledlayout calls appear to be interfering with each other.
Commenting-out the tiledlayout calls produces this —
clear all; close all; clc;
data= [0.1,3.8;
0.1,2.7;
0.01,2.3;
%0.1,2.7;
0.01,0.7;
0.1,3.3;
0.07,4.7;
0.02,0;
0.01,1;
%0.07,4.7;
0.23,3.4;
%0.07,4.7;
%0.07,4.7;
0.1,5.8;
%0.07,4.7;
0.01,4.7;
0.1,0.5;
%0.1,5.8;
0.23,3.1;
0.01,5.9;
0.02,4.2;
0.1,5.9;
0.07,1.5];
subplot (1,2,1)
x = data(:,1);
y = data(:,2);
dy=0.2;
%f=figure;
% t=tiledlayout("flow");
% nexttile(t);
h=scatter(x,y,100,'filled','MarkerEdgeColor','k');
C = jet(numel(x));
h.CData = C;
set(gca, 'colormap', C)
colorbar()
%xlabel('${\it} (A^{2}/B)^{1/4}$','Interpreter','Latex')
xlabel('${\omega }$','Interpreter','Latex')
ylabel('${\varphi }$','Interpreter','Latex')
labels=["d1","d2,d4","d3","d5","d6","d7,p1,p3,p4,v2","d8","d9","p2","v1,v5","v3","v4","v6","v7","v8","v9","v10"];
text(x,y+dy,labels,"HorizontalAlignment","left","VerticalAlignment","cap",'FontSize',10);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data= [0.1,3.8;
0.1,2.7;
0.01,2.3;
%0.1,2.7;
0.01,0.7;
0.1,3.3;
0.07,4.7;
0.02,0;
0.01,1;
%0.07,4.7;
0.23,3.4;
%0.07,4.7;
%0.07,4.7;
0.1,5.8;
%0.07,4.7;
0.01,4.7;
0.1,0.5;
%0.1,5.8;
0.23,3.1;
0.01,5.9;
0.02,4.2;
0.1,5.9;
0.07,1.5];
subplot (1,2,2)
x = data(:,1);
y = data(:,2);
dy=0.2;
%f=figure;
% t=tiledlayout("flow");
% nexttile(t);
h=scatter(x,y,100,'filled','MarkerEdgeColor','k');
C = jet(numel(x));
h.CData = C;
set(gca, 'colormap', C)
colorbar()
%xlabel('${\it} (A^{2}/B)^{1/4}$','Interpreter','Latex')
xlabel('${\omega }$','Interpreter','Latex')
ylabel('${\varphi }$','Interpreter','Latex')
labels=["d1","d2,d4","d3","d5","d6","d7,p1,p3,p4,v2","d8","d9","p2","v1,v5","v3","v4","v6","v7","v8","v9","v10"];
text(x,y+dy,labels,"HorizontalAlignment","left","VerticalAlignment","cap",'FontSize',10);
Is this what you want?
.
카테고리
도움말 센터 및 File Exchange에서 Subplots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


