Clear Filters
Clear Filters

How to add axis in a figure where I'm drawing lines.

2 views (last 30 days)
This is the code I'm using.
I = imread('figure251.png');
Error using imread>get_full_filename
File "figure251.png" does not exist.

Error in imread (line 371)
fullname = get_full_filename(filename);
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];
hold on
p3 = [130.5,125.5];
p4 = [130.5,130.5];
hold on
p5 = [125.5,125.5];
p6 = [130.5,125.5];
hold on
p7 = [125.5,130.5];
p8 = [130.5,130.5];
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.

Accepted Answer

DGM on 11 Jun 2022
Edited: DGM on 11 Jun 2022
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('');
xylim = [-90,90].*4.65./1000;
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.
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.
DGM on 11 Jun 2022
Edited: DGM on 11 Jun 2022
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('');
xylim = [-90,90].*4.65./1000;
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
fs = 16; fs2 = 25;
h = colorbar;
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);
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];
U B on 11 Jun 2022
Thank you so much for the explanation and the solution.

Sign in to comment.

More Answers (0)


Find more on Display Image 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!