matlab imshow not fitting the defined axes

15 views (last 30 days)
Hello,
I have to generate a video made of three main objects coming from some data previously generated:
1)patch graph of a thickness
2)a grey-scale image
3)a plot
I generated three axes: `ax1`,`ax2`,`ax3` with their properties. When I put the image inside ax2, I always get an image that is smaller, centred, and scaled.
This is what i get:
the upper patch graph and the image should be of the same length but I'm not able to figure out how.
Part of the code:
fig=figure('visible','on');
fig.Position=([0 0 900 500]);
pipeLength=371;
limY_TBS=400;
img=[];
x=(1:pipeLength);
xp=[x flip(x)];
z=[];
x1=[-1 x x(end)+1 x(end)+1 flip(x) -1];
y=ones(1,numel(x1));
y(1:end/2)=y(1:end/2)*(limY_TBS+10);
y(end/2+1:end)=-1*y(end/2+1:end);
ax1=axes(fig,'Position',[0.1 0.60 0.80 0.3]);
p1=patch(ax1,x1,y,ones(length(x1),1),'FaceColor','interp');
hc=colorbar(ax1,'Location','manual','Position',[0.91 0.6 0.02 0.3]);
title(hc,'°C');
xticks(ax1,0:50:350)
xticklabels(ax1,["0","35","70","105","140","175","210","245 [mm]"])
set(ax1,'TickDir','out')
xlim(ax1,[0 pipeLength])
yticks(ax1,0:25:limY_TBS)
yticklabels(ax1,["0","","","","100","","","","200","","","","300","","","","400"])
clim(ax1,[20 40]);
patchHeigth=limY_TBS;
colormap(ax1,'turbo')
p2=patch(ax1,x1,y,'w');
ylim([0 limY_TBS]);
ylabel("Film Thickness [" + char(181)+ "m]",'FontSize',15),
ax2=axes(fig,'Position',[0.1 0.20 0.8 0.3]);
mfile=matfile(Vid(1).path);
mOnes=size(cell2mat(mfile.newIred(1,1)));
imgGraph=imshow(mOnes,'Parent', ax2,'XData',[0 size(cell2mat(mfile.newIred(1,1)),2)],...
'YData',[-10 size(cell2mat(mfile.newIred(1,1)),1)],...
'initialMagnification','fit');
imgGraph.CDataMapping="direct";
t1=text(ax2,1,200,' ','FontSize',15,'FontSmoothing', 'on'); %time text
t2=text(ax2,600,200,' ','FontSize',15,'FontSmoothing', 'on'); %acceleration text
t3=text(ax2,300,200,' ','FontSize',15,'FontSmoothing', 'on'); %power text
ax3=axes(fig,'Position',[0.1 0.05 0.8 0.1]); %axis for the acceleration graph
I would like that the image on ax2 to completely fit the defined axes to have the same horizontal span of the top graph.
Thank you for any support that you can give.

Accepted Answer

Image Analyst
Image Analyst on 10 Jul 2022
You need to change the aspect ratio of the middle axes control. Evidently it's much wider than tall. So it puts the image in and starts to zoom up the image but it gets just that tall and then can't magnify the image anymore without distorting the aspect ratio. If it did stretch it out the full width of the control, you'd have an image with the same height but abnormally stretched in the horizontal direction.
I think imshow might put the 'image' property to the axes so that pixels remain square. If you're okay with it being really stretched horizontally you might look at the other options that the axis function provides.
  1 Comment
Riccardo Clavenna
Riccardo Clavenna on 11 Jul 2022
Edited: Riccardo Clavenna on 11 Jul 2022
Thank you!
I set the height and length of the ax2 to be equal and then modifies it with XData and YData properties to change the position and the size and it worked
Here what I've changed:
ax2=axes(fig,'Position',[0.1 0.15 0.8 0.8]);
----
imgGraph.XData=[-325,990];
imgGraph.YData=[100,200];

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!