How to re-scale an axis

404 views (last 30 days)
Kristina
Kristina on 11 Dec 2022
Commented: Star Strider on 12 Dec 2022
I've been searching this community for this answer, but I don't believe what has been suggested to others helps in what I'm trying to accomplish.
The code I have does produce what I am looking to see in terms of a graph, but it sets the axis to the 6th power of 10. The original graph uses seconds. I am trying to rescale the horizontal axis so that it doesn't dictate it as 10 to the 6th. ylim, set gca, and caxis do not actually accomplish what I am trying to do. For reference I attached what my graph looks like and the black/white graph is what I am trying to accomplish. You can see one has a horizontal axis that only goes to 20 seconds (as its supposed to) but mine in the blue goes to 2,000,000. I thought the math worked out if I said this was in milliseconds but I have realized that doesn't make sense so please ignore the label on my graph that says (ms).

Accepted Answer

Star Strider
Star Strider on 11 Dec 2022
Try something like this —
x = linspace(0, 2E6, 2E6);
y = sin(2*pi*x/1E6);
figure
plot(x, y)
figure
plot(x, y)
xt = get(gca, 'XTick');
set(gca,'XTickLabel',xt*1E-5)
The x-tick values are unchanged, however the x-tick labels have been re-scaled.
.
  2 Comments
Kristina
Kristina on 12 Dec 2022
Thank you so much!
Star Strider
Star Strider on 12 Dec 2022
As always, my pleasure!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 11 Dec 2022
So 2 million milliseconds is 2E-3 * 1E6 is 2E+3 seconds or 2000 seconds. So you can divide your x by 10^6 so that you get numbers in the 0 to 2 range and then just update your label:
plot(x/1E6, y, 'b-');
xlabel('Time (thousands of seconds)');

Categories

Find more on Graphics Objects 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!