How can I change the tick values on x axis?

22 ビュー (過去 30 日間)
Pul
Pul 2021 年 12 月 8 日
コメント済み: Pul 2021 年 12 月 8 日
Hello everyone,
I would like to put the tick label on x axis every 2 years instead of every 5 years. How can I do it?
Thank you!
LD = load('MODESTAMMEQ.mat');
MODESTAMMEQ = LD.MODESTAMMEQ;
dv = regexp(MODESTAMMEQ.Jan1989, '\d*\-\w*\-\d*','match');
MODESTAMMEQ.Var1 = datetime([dv{:}], 'InputFormat',"dd-MMM-yyyy").';
x = MODESTAMMEQ.Var1;
y = MODESTAMMEQ.Var2*10*0.35;
figure
plot(x, y)
grid

採用された回答

Peter Bonavita
Peter Bonavita 2021 年 12 月 8 日
Hi Pul,
You can use xticks to specify the values where you want the ticks to appear, and xticklabels to customize the labels themselves, if you wish.
In your case, you'll specify something like xticks([0 5 10 15]), etc.
Thanks,
Peter
  4 件のコメント
Peter Bonavita
Peter Bonavita 2021 年 12 月 8 日
Hi Pul, this should get you started.
First, check out the value of xticks from your original plot:
>> ticks = xticks
ticks =
1×4 datetime array
2000 2005 2010 2015
Create a datetime array as Rik suggested:
newTicks = datetime({'2000','2002','2004','2006'},'InputFormat','u');
Use this to update the labels on the plot with xticks:
>> xticks(newTicks)
Pul
Pul 2021 年 12 月 8 日
Thank you @Peter Bonavita.
The only problem is that then I can't plot my y with the new x, because they have a different length.
newTicks = datetime({'2000','2002','2004','2006','2008','2010','2012','2014','2016','2018'},'InputFormat','u');
xticks(''2000','2002','2004','2006','2008','2010','2012','2014','2016','2018');

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGrid Lines, Tick Values, and Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by