フィルターのクリア

How to use separate locations for axis location and axis labels?

2 ビュー (過去 30 日間)
Omar Mian
Omar Mian 2022 年 2 月 27 日
コメント済み: Omar Mian 2022 年 3 月 6 日
Hello,
I'd like to create a 2D plot where the x-axis axis is locatatd at y = 0 but the labels stay at the bottom of the plot rather than close to the axis (relevant when y-axis lower limit is less than zero). I want to achieve something like this (produced in Excel):
In Matlab I can place the x-axis at y=0 using the 'XAxisLocation' property
figure
time = [0 1 2 3 4 5];
value = [-3 -1 4 1 2 5];
plot(time,value)
xlabel('time'), ylabel('value')
set(gca,'ylim',[-5 5])
set(gca,'XAxisLocation','origin')
But this also moves the x-axis labels to the middle of the plot.
Is there a way to keep the x-axis labels at the bottom?
Thanks

採用された回答

Scott MacKenzie
Scott MacKenzie 2022 年 2 月 27 日
編集済み: Scott MacKenzie 2022 年 2 月 27 日
You don't need to set the x-axis location. Delete that line and then use yline to get a line at y = 0:
figure
time = [0 1 2 3 4 5];
value = [-3 -1 4 1 2 5];
plot(time,value);
xlabel('time'), ylabel('value')
set(gca,'ylim',[-5 5])
yline(0); % add line at y=0
% add tick marks
ax = gca;
x = ax.XTick;
for i=1:numel(x)
line([x(i) x(i)],[0.1 -0.1], 'color', [.4 .4 .4]);
end
  3 件のコメント
Scott MacKenzie
Scott MacKenzie 2022 年 2 月 27 日
OK, yes, I see the ticks now. I just added some code to draw tick marks.
Omar Mian
Omar Mian 2022 年 3 月 6 日
Thank you. I was hoping there might be an axis property that I had overlooked, but I can use this as a workaround.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLabels and Annotations についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by