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 commentaires

Ameer Hamza
Ameer Hamza le 11 Mar 2020
How are you displaying the image? By default imshow() does not display axes.
Josh Bridges
Josh Bridges le 11 Mar 2020
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.
Ameer Hamza
Ameer Hamza le 11 Mar 2020
You can make the axis invisible again. See my answer. Now, how to synchronize these both axis is a seperate issue.
Josh Bridges
Josh Bridges le 11 Mar 2020
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.
Ameer Hamza
Ameer Hamza le 11 Mar 2020
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.
Ameer Hamza
Ameer Hamza le 12 Mar 2020
Josh, check the code in my updated answer.

Connectez-vous pour commenter.

 Réponse acceptée

Ameer Hamza
Ameer Hamza le 11 Mar 2020
Modifié(e) : Ameer Hamza le 12 Mar 2020

0 votes

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 commentaires

Josh Bridges
Josh Bridges le 13 Mar 2020
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.
Ameer Hamza
Ameer Hamza le 13 Mar 2020
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
Josh Bridges
Josh Bridges le 13 Mar 2020
Thanks for this!
I'll try and apply it to the above plot.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by