Matlab figure: Replace xaxis with custom labels

8 ビュー (過去 30 日間)
Travis Head
Travis Head 2023 年 8 月 20 日
コメント済み: Adam Danz 2023 年 8 月 20 日
I have a figure and would like to replace the x axis of the plot to a custom one. So now there are the values from 0 to 300 on the x axis, but I need it to show the values from 1 to 10 in a equal distance on the whole width of the figure. How do I do that?
Here are the figure and the code, if this helps:
sun = dataset.sun
%time = dataset.time
figure
plot(sun)
So far I tried some options for manipulating the existing x axis, but not really succesfull so far.
  1 件のコメント
Adam Danz
Adam Danz 2023 年 8 月 20 日
When you use the plot(y) syntax, MATLAB generates the x-values 1:n where n is the number of values in y.
Instead, define x as @Sam Chak demonstrated and us plot(x,y).

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

回答 (2 件)

Sam Chak
Sam Chak 2023 年 8 月 20 日
編集済み: Sam Chak 2023 年 8 月 20 日
Is the following workaround acceptable?
time = linspace(0, 30, 301); % original x-axis
sun = tanh(1/3*(time - 15));
plot(sun), grid on, xlim([0 300])
xlabel('data points'),
% generate 2nd x-axis to have the same number of elements in 'sun'
x2 = linspace(1, 10, numel(sun));
plot(x2, sun), grid on, xlim([1 10])
xlabel('redefined x-axis'),

Star Strider
Star Strider 2023 年 8 月 20 日
Another approach —
sun = sin((0:240)*pi/25).^2 .* exp(-0.015*(0:240));
figure
plot(sun)
figure
plot(sun)
xt = xticks;
xticks(linspace(min(xt), max(xt), 10));
xticklabels(1:10)
.

カテゴリ

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