How to add axis in a figure where I'm drawing lines.
조회 수: 1 (최근 30일)
이전 댓글 표시
This is the code I'm using.
I = imread('figure251.png');
figure, imshow(I); colormap hot ;
axis('on', 'image');
xticklabels({'-10','-5','0','5','10'}) ;
yticklabels({'10','5','0','-5','-10'}) ;
hold on
p1 = [125.5,125.5];
p2 = [125.5,130.5];
plot([p1(1),p2(1)],[p1(2),p2(2)],'Color','w','LineWidth',3)
hold on
p3 = [130.5,125.5];
p4 = [130.5,130.5];
plot([p3(1),p4(1)],[p3(2),p4(2)],'Color','w','LineWidth',3)
hold on
p5 = [125.5,125.5];
p6 = [130.5,125.5];
plot([p5(1),p6(1)],[p5(2),p6(2)],'Color','w','LineWidth',3)
hold on
p7 = [125.5,130.5];
p8 = [130.5,130.5];
plot([p7(1),p8(1)],[p7(2),p8(2)],'Color','w','LineWidth',3)
I need to put axis in the above program. But if I zoom in then the no of points in the axis reduces. if I use axis on only then it coms as piel no. Then I can zoom in. I want to zoom in with more axis label.
댓글 수: 0
채택된 답변
DGM
2022년 6월 11일
편집: DGM
2022년 6월 11일
You can set xdata,ydata for imshow() as you do with image()/imagesc(), but by default imshow() will turn off axis visibility. You can turn it back on, but then you have to deal with the fact that the figure has been set up without consideration for the area required to show ticks and axis labels. You may have to adjust the position of the axes object to make room for everything.
I = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1028490/image.png');
xylim = [-90,90].*4.65./1000;
imshow(I,'Xdata',xylim,'Ydata',xylim);
set(gca,'visible','on')
colormap(hot);
The utility of imshow() is its conveniences for viewing pictures without extraneous axis decorations. If you're using it with the axis visible, I'm not sure why you wouldn't just use image()/imagesc() instead. You might need to set axis equal if you do, but that's about it.
https://www.mathworks.com/matlabcentral/answers/1288-what-is-the-difference-between-image-and-imshow
FWIW, the first answer is a bit misleading. Both image() and imshow() will display RGB images in truecolor and they'll both display grayscale images using a colormap. The only difference is that imshow() forces the use of a gray colormap instead of whatever map was being used in the axes at the time. Again, that's considered a matter of convenience. If you explicitly set the map after the the image is displayed, there really isn't any difference.
댓글 수: 5
DGM
2022년 6월 11일
편집: DGM
2022년 6월 11일
The points you're drawing are outside the axes limits that were explicitly set. If you want the ticks to be in a specific range and you want everything to stay aligned with zooming, I'd probably just scale/offset the points you're plotting. It'd simplify the plotting to express all the points as lines (the rows of a matrix).
I = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1028490/image.png');
xylim = [-90,90].*4.65./1000;
imshow(I,'Xdata',xylim,'Ydata',xylim);
set(gca,'visible','on')
colormap(hot);
hold on
% each row corresponds to a line
X = [125.5 125.5; 130.5 130.5; 125.5 130.5; 125.5 130.5];
Y = [125.5 130.5; 125.5 130.5; 125.5 125.5; 130.5 130.5];
% scale values to a new nominal range
% note that the input range will accomodate a nonsquare image
% but the output range (xylim) assumes the image is square
% idk if that will ever matter for your use
X = scalepts(X,[1 size(I,2)],xylim);
Y = scalepts(Y,[1 size(I,1)],xylim);
% plot all line segments
plot(X,Y,'Color','w','LineWidth',3)
fs = 16; fs2 = 25;
h = colorbar;
ylabel(h,'$Ix/I_{max}$','interpreter','latex','fontsize',fs2);
set(gca,'fontsize',fs,'xlim',xylim,'ylim',xylim); axis square
xlabel('$x$ (mm)','interpreter','latex','fontsize',fs2);
ylabel('$y$ (mm)','interpreter','latex','fontsize',fs2);
function outpts = scalepts(inpts,inrange,outrange)
outpts = (inpts-inrange(1))/range(inrange)*range(outrange)+outrange(1);
end
Alternatively, you can simplify the plotting further. Instead of plotting a set of four individual line segments, you can plot a single polyline that forms a rectangle.
X = [125.5 125.5 130.5 130.5 125.5];
Y = [125.5 130.5 130.5 125.5 125.5];
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!