Set Image Axis as Primary Axis

I have a figure of a plot/graph I would like to load into MATLAB (first figure below) that has its own axes. I would like to make these axes my working axes as I need to plot data on top of this graph at particular points corresponding to the figure's axis. However, when I load the image, MATLAB generates it's own axis(second figure). If it wasn't for the fact that the figure axis is on a log-log scale, this might not be a problem.
Any help would be appreciated it. Thanks!

6 Comments

How are you displaying the image? By default imshow() does not display axes.
Hi Ameer,
I'm using imshow but then using 'axis on' to see where MATLAB is putting its axis to see how the two line up.
You can make the axis invisible again. See my answer. Now, how to synchronize these both axis is a seperate issue.
Hi Ameer,
Yeah, the synchronization is my issue.
My data needs to be plotted on the figure and the axis that MATLAB generates won't work.
I recently answered another question dealing with synchronization of axes: https://www.mathworks.com/matlabcentral/answers/509899-plot-with-two-related-x-axes. Although these two cases are not different, however, the basic idea is the same. You can see that answer to come up with a solution. If no one else posted a solution until tomorrow, then I will try to post a solution.
Josh, check the code in my updated answer.

Sign in to comment.

 Accepted Answer

Ameer Hamza
Ameer Hamza on 11 Mar 2020
Edited: Ameer Hamza on 12 Mar 2020
Following example show how to synchronize the image axis with the MATLAB plot axis
im = imread('image.jpeg');
fig = figure();
h = imshow(im);
ax_img = gca;
ax_plot = axes();
hold(ax_plot);
ax_plot.Color = 'none';
ax_plot.Position = [0.183 0.158 0.678 0.778]; % bit of trial and error
ax_plot.XLim = [1 100];
ax_plot.YLim = [0.005 1];
ax_plot.XScale = 'log';
ax_plot.YScale = 'log';
% Example: give values in image coordinates
plot(ax_plot, [1 5 10 50 100], [0.005, 0.05, 0.1, 0.5, 1], 'r', 'LineWidth', 2);
ax_plot.Visible = 'off';

4 Comments

Hi Ameer,
Thanks for this!
I had to play around a but with the image size and axes properties to get them to line up perfectly but this definitely gave me what I wanted.
I don't know if I'm allowed to do this or not(first time posting here), but do you know how to turn off the axes but keep the labels? I've tried playing around with the XRuler and YRuler but it doesn't seem to like this script.
Are you talking about ticks or label? Although not possible using axes properties, you can use annotation() to add textboxes to create ticks and label.
Here is an example for the case when you want to make the axis invisible but keep the labels.
fig = figure();
ax = axes();
hold(ax);
plot(1:10);
ax.XAxis.Visible = 'off';
x_position = cumsum(ax.Position([1 3]));
y_position = ax.Position(2);
num_ticks = numel(ax.XAxis.TickValues);
x_tickLabels = ax.XAxis.TickLabels;
ticks_x_position = linspace(x_position(1)-0.01, x_position(2)-0.02, num_ticks); % subtraction values are based on manual tuning
ticks_y_position = y_position - 0.1;
for i=1:num_ticks
annotation(fig, 'textbox', [ticks_x_position(i), ticks_y_position 0.1 0.1], ...
'String', x_tickLabels{i}, ...
'EdgeColor', 'none');
end
Thanks for this!
I'll try and apply it to the above plot.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!