How to plot two figures side by side in a same plot?
45 views (last 30 days)
Show older comments
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 Comment
Accepted Answer
Hassaan
on 25 Jan 2024
Edited: Hassaan
on 25 Jan 2024
clear all; close all; clc;
% Dummy data
% data = rand(22, 2); % 22 rows of random data
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];
% First subplot
subplot(1, 2, 1);
x = data(:, 1);
y = data(:, 2);
dy = 0.2;
h = scatter(x, y, 100, 'filled', 'MarkerEdgeColor', 'k');
C = jet(numel(x));
h.CData = C;
colormap(gca, C); % Apply colormap to current axes
colorbar();
xlabel('${\omega }$', 'Interpreter', 'Latex');
ylabel('${\varphi }$', 'Interpreter', 'Latex');
% Second subplot
subplot(1, 2, 2);
h = scatter(x, y, 100, 'filled', 'MarkerEdgeColor', 'k');
h.CData = C;
colormap(gca, C); % Apply colormap to current axes
colorbar();
xlabel('${\omega }$', 'Interpreter', 'Latex');
ylabel('${\varphi }$', 'Interpreter', 'Latex');
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
0 Comments
More Answers (2)
Adam Danz
on 25 Jan 2024
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);
0 Comments
Star Strider
on 25 Jan 2024
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?
.
0 Comments
See Also
Categories
Find more on Green 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!