How can I get the right image ratio when displaying an image behind a graph?

2 visualizzazioni (ultimi 30 giorni)
I want to display an image behind the graph and would like to have to it as a specific X1 vlaue and length which is given by the X2. When I plot it like in my code the image is oversized along the y-axis and it would be nice to get the original ratios of the image. I know how long the object on the image is and I want to scale this to the x-axis (with the X1 and X2) and keep the right widht of the image.
X1 = X
X2 = X+590
figure
a1 = gca;
hold on
image('CData', imread('20-1.jpg'), 'XData', [X1 X2]);
plot(Pos9, TC_V9, '-', 'LineWidth', 2, 'Color', [0 0 0]);
ylabel('TC [W/m/K]', 'Fontweight', 'bold', 'FontSize', 14)
xlabel('Position (mm)', 'Fontweight', 'bold', 'FontSize', 14)
title('C20-1 (67-98 cm) Calc-Sil', 'FontSize', 16)
hold off

Risposte (1)

Walter Roberson
Walter Roberson il 26 Mag 2023
The rule is that the XData you provide will be used as the center of the pixels. You want to shift the boundaries by half of a pixel each.
img = imread('20-1.jpg');
numcol = size(img,2);
X1 = X + numcol/2;
X2 = X + 590 - numcol/2;
fig = figure();
a1 = axes('Parent', fig);
hold(a1, 'on');
image(a1, 'CData', img, 'XData', [x1 X2]);
plot(a1, Pos9, TC_V9, '-', 'LineWidth', 2, 'Color', [0 0 0]);
ylabel(a1, 'TC [W/m/K]', 'Fontweight', 'bold', 'FontSize', 14)
xlabel(a1, 'Position (mm)', 'Fontweight', 'bold', 'FontSize', 14)
title(a1, 'C20-1 (67-98 cm) Calc-Sil', 'FontSize', 16) )
hold(a1, 'off')
  1 Commento
Kim
Kim il 27 Mag 2023
Thanks for the answer. It is not exactly what I am searching for. In the meantime I updated my code and this works quiet well, but the problem is that I have to define the values ymin and ymax to fit the image to the y-axis. BUT my question is: is it possible not set the x-values and not the y-values? Because the image doesn´t have the right aspect ratio when I am using the code in that way. Is it possible to size the image by only using the x-values, that the image has its original ratio of length and heigth by defining the length (here with the x-values) ? And I cannot use the x-values for that because they have another dimension as the y-values of the graph otherwise I would not see the graph properly.
Thanks !
Here my updated code:
F1 = imread('20-1.jpg');
X1 = X+590;
yMin = min(TC_V9);
yMax = max(TC_V9);
figure
a1 = gca;
imagesc([X X1], [yMin yMax], F1);
set(a1, 'Color', 'none');
hold on
plot(Pos9, TC_V9)
ylabel('TC [W/m/K]', 'Fontweight', 'bold', 'FontSize', 14)
xlabel('Position (mm)', 'Fontweight', 'bold', 'FontSize', 14)
title('C20-1', 'FontSize', 16)

Accedi per commentare.

Categorie

Scopri di più su Images in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by