Create custom x-axis for 'imagesc' plot

408 ビュー (過去 30 日間)
Kevin Mutai
Kevin Mutai 2020 年 7 月 4 日
コメント済み: Kevin Mutai 2020 年 7 月 4 日
I am trying to create a custom x-axis for my imagesc plot using a separate vector ('xaxis') with increasing values from -150.36 to 265.8773.
However, I am facing an issue where when I set this vector as my x-axis, it appears only for half of the image. The vector 'xaxis' has the same length as the x-axis pixels in the image and I would like it's values to represent each x-axis pixel rather than the default which only has the pixel value listed.
My intention is to then evenly space out the x-axis ticks using the label command after setting the x-axis to 'xaxis' to avoid overwriting/bunching together.
Original x-axis;
x-axis from vector 'xaxis':
From the image, I understand that the ticks only go to half the image before starting over and overwriting the previous ticks.
Any tips would be highly appreciated.
Below is my code for setting the x-axis to the desired tick values;
colormap gray;
%subplot(2,2,1);
imagesc(inclinedCyl_d20);
ax = gca;
ax.XTick = [xaxis];

採用された回答

Star Strider
Star Strider 2020 年 7 月 4 日
Much of your code is ‘over the horizon’ and so out of sight.
Try something like this:
x = 0:500; % Create Data
y = randn(size(x)); % Create Data
figure
plot(x, y)
xt = get(gca, 'XTick'); % Original 'XTick' Values
xtlbl = linspace(-150.36, 265.8773, numel(xt)); % New 'XTickLabel' Vector
set(gca, 'XTick',xt, 'XTickLabel',xtlbl, 'XTickLabelRotation',30) % Label Ticks
I know you are displaying an image, not data, however this should work. Note that you need to define the location of the x-tick labels with respect to the original x-tick values. They do not have to be the actual x-tick values (as I use here), however they must be within that range.
Something like this would also work:
xtnew = linspace(min(xt), max(xt), 5); % New 'XTick' Values
xtlbl = linspace(-150.36, 265.8773, numel(xtnew)); % New 'XTickLabel' Vector
set(gca, 'XTick',xtnew, 'XTickLabel',xtlbl) % Label Ticks
Note that ‘xtnew’ spans the original ‘xt’ range. It just defines them differently.
.
  2 件のコメント
Kevin Mutai
Kevin Mutai 2020 年 7 月 4 日
Hi Star Strider (cool name by the way), thanks for the tip. It worked great!
Star Strider
Star Strider 2020 年 7 月 4 日
As always, my pleasure!
Thank you!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 7 月 4 日
If you use imshow(), you can simply pass in the endpoints of the x axis to the 'XData' property:
imshow('moon.tif', 'XData', [-150.36, 265.8773]);
  3 件のコメント
Image Analyst
Image Analyst 2020 年 7 月 4 日
Of course:
imshow(inclinedCyl_d20, [], 'XData', [-150.36, 265.8773]);
Kevin Mutai
Kevin Mutai 2020 年 7 月 4 日
Wonderful, thanks for the tip. This works too.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by