How to have a same size of the image with out distortion in a subplot along with the graph?

4 views (last 30 days)
Hello all,
I am trying to place an image along with a plot in subplot. But when i am trying to plot the image is small compared to the size of the plot. I need both of them to be in the same size. I also used "imagesc(img);" , but the image looks distorted. Please help me with this. Thanks in advance.
My code:
set(0,'Units','pixels');
scrsz = get(0,'ScreenSize');
scr_width = scrsz(3);
scr_heigth = scrsz(4);
img = imread('Geometry.jpg');
Z = readtable('Fig1.xlsx');
alpha = 0.1;
figure('Position', round([alpha*scr_width alpha*scr_heigth (1-2*alpha)*scr_width (1-2*alpha)*scr_heigth ]));
% 1. Image subplot
subplot(1,4,1);
% imagesc(img);
imshow(img);
axis off
data = table2array(Z) ;
subplot(1,4,2)
plot(data(:,1), data(:,2), 'k','linewidth', 2);
hold off
grid on
xlabel('xxx');
ylabel('yyy');
subplot(1,4,3)
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
str = '#0072BD';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,5), data(:,6), '*', 'Color', color, 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold on
str = '#EDB120';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,9), data(:,10), '*', 'Color', color, 'linewidth', 2);
hold on
plot(data(:,11), data(:,12), 'linewidth', 2);
hold on
str = '#77AC30';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,13), data(:,14), '*', 'Color', color, 'linewidth', 2);
hold on
plot(data(:,15), data(:,16), 'linewidth', 2);
hold on
str = '#A2142F';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,17), data(:,18), '*', 'Color', color, 'linewidth', 2);
hold off
% legend('Wygnanski&Fiedler (1969)', 'Standard k-\in', 'RNG k-\in', 'Realizable k-\in',...
% 'SST k-\omega', 'Location', 'northeast');
grid on
xlabel('xxx');
ylabel('yyy');
subplot(1,4,4)
plot(data(:,19), data(:,20), 'linewidth', 2);
hold on
str = '#0072BD';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,21), data(:,22), '*', 'Color', color, 'linewidth', 2);
hold on
plot(data(:,23), data(:,24), 'linewidth', 2);
hold on
str = '#EDB120';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,25), data(:,26), '*', 'Color', color, 'linewidth', 2);
hold on
plot(data(:,27), data(:,28), 'linewidth', 2);
hold on
str = '#77AC30';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,29), data(:,30), '*', 'Color', color, 'linewidth', 2);
hold on
plot(data(:,31), data(:,32), 'linewidth', 2);
hold on
str = '#A2142F';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,33), data(:,34), '*', 'Color', color, 'linewidth', 2);
hold off
% legend('Wygnanski&Fiedler (1969)', 'Standard k-\in', 'RNG k-\in', 'Realizable k-\in',...
% 'SST k-\omega', 'Location', 'northeast');
grid on
xlabel('xxx');
ylabel('yyy');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.

Accepted Answer

Mathieu NOE
Mathieu NOE on 4 Jul 2025
hello
there is a very detailled example here how to manage subplots dimensions (if you want to master it)
here I offer a quick and simple solution by making the figure "wider" so that both subplots appear more alike in size
here's my result so far :
I didn't notice any difference here between imagesc and imshow
set(0,'Units','pixels');
scrsz = get(0,'ScreenSize');
scr_width = scrsz(3);
scr_heigth = scrsz(4);
img = imread('Geometry.jpg');
Z = readtable('trail.xlsx');
alpha = 0.1;
figure('Position', round([alpha*scr_width alpha*scr_heigth (1-2*alpha)*scr_width (1-2*alpha)*scr_heigth ]));
% 1. Image subplot
subplot(1,2,1);
% imagesc(img);
imshow(img);
axis off
data = table2array(Z) ;
subplot(1,2,2)
plot(data(:,1), data(:,2), 'k','linewidth', 2);
hold off
grid on
xlabel('xxx');
ylabel('yyy');
%% optionnal (try)
% pbaspect([1 1 1]) % Make the x-axis, y-axis equal lengths
% daspect([1 1 1]); % equal lengths in all directions
  5 Comments
Vishnuvardhan Naidu Tanga
Thank you for the response. As you can see the size of the image and the plots are not same. How can I have both width and height of all the plots and image same?
Mathieu NOE
Mathieu NOE on 6 Jul 2025
Edited: Mathieu NOE on 7 Jul 2025
hello again
tried to find a good balance ... you can opt for the automatic tuning or the manual settings (factor "a" that allows a different width to the image plot axis) - see what works for you
I believe with a = 1.5 I have the best rendering :
img = imread('Geometry.jpg');
[m,n] = size(img);
h2w_ratio = n/m;
Z = readtable('Fig1.xlsx');
data = table2array(Z) ;
%%
f = figure('Units','normalized','Position', [0.1 0.1 0.8 0.8]);
height = 0.6;
hs = 0.05; % horizontal separation
left = hs;
width = 1-2*left;
bottom = 0.5 - height/2;
N = 4; % nb of subplots = axes
%% increase width of first axe by factor a
% if we want to equal height to width ratios between image and plot axes , this
% means for the 1st axe : height/(a*w) = h2w_ratio
% combined with : w = (width - (N-1)*hs)/(a + (N-1));
% we obtain the optimal a value as :
a = height/h2w_ratio*(N-1)/(width - (N-1)*hs - height/h2w_ratio); % automatic tuning
a = 1.5; % manual tuning
w = (width - (N-1)*hs)/(a + (N-1));
% create first axe with specific dimensions
ax(1) = axes(f,'Position',[left bottom a*w height]);
left = left + a*w + hs;
% then the rest with same dims
for k = 2:N
ax(k) = axes(f,'Position',[left bottom w height]);
left = left + w + hs;
end
% 1. Image subplot
% subplot(1,4,1);
imshow(img, 'Parent', ax(1));
axis off
% subplot(1,4,2)
axes(ax(2)); % Activate the 2nd axes
plot(data(:,1), data(:,2), 'k','linewidth', 2);
hold off
grid on
xlabel('xxx');
ylabel('yyy');
% subplot(1,4,3)
axes(ax(3)); % Activate the 3rd axes
plot(data(:,3), data(:,4), 'linewidth', 2);
hold on
str = '#0072BD';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,5), data(:,6), '*', 'Color', color, 'linewidth', 2);
hold on
plot(data(:,7), data(:,8), 'linewidth', 2);
hold on
str = '#EDB120';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,9), data(:,10), '*', 'Color', color, 'linewidth', 2);
hold on
plot(data(:,11), data(:,12), 'linewidth', 2);
hold on
str = '#77AC30';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,13), data(:,14), '*', 'Color', color, 'linewidth', 2);
hold on
plot(data(:,15), data(:,16), 'linewidth', 2);
hold on
str = '#A2142F';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,17), data(:,18), '*', 'Color', color, 'linewidth', 2);
hold off
% legend('Wygnanski&Fiedler (1969)', 'Standard k-\in', 'RNG k-\in', 'Realizable k-\in',...
% 'SST k-\omega', 'Location', 'northeast');
grid on
xlabel('xxx');
ylabel('yyy');
% subplot(1,4,4)
axes(ax(4)); % Activate the 4th axes
plot(data(:,19), data(:,20), 'linewidth', 2);
hold on
str = '#0072BD';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,21), data(:,22), '*', 'Color', color, 'linewidth', 2);
hold on
plot(data(:,23), data(:,24), 'linewidth', 2);
hold on
str = '#EDB120';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,25), data(:,26), '*', 'Color', color, 'linewidth', 2);
hold on
plot(data(:,27), data(:,28), 'linewidth', 2);
hold on
str = '#77AC30';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,29), data(:,30), '*', 'Color', color, 'linewidth', 2);
hold on
plot(data(:,31), data(:,32), 'linewidth', 2);
hold on
str = '#A2142F';
color = sscanf(str(2:end),'%2x%2x%2x',[1 3])/255;
plot(data(:,33), data(:,34), '*', 'Color', color, 'linewidth', 2);
hold off
% legend('Wygnanski&Fiedler (1969)', 'Standard k-\in', 'RNG k-\in', 'Realizable k-\in',...
% 'SST k-\omega', 'Location', 'northeast');
grid on
xlabel('xxx');
ylabel('yyy');

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!