change the xticklabel with date format

I want to show all dates in the x-axis of the below fig. (jan 71, jan 72, ...)
and please tell me how I can remove the labels of second y axis after 20 (the black circles) ?
could you please help me.
Eforc=load('forc.txt');
P=Eforc(:,5);
Q=Eforc(:,4);
date = datenum(Eforc(366:end ,1:3));
[ax,h1,h2] = plotyy(date,Q(366:end),date,P(366:end),'plot', 'bar');
set(ax(2), 'Ydir', 'reverse','YTick',[0,10,20]);
ylim(ax(1), [0 3]);
set(h2,'EdgeColor','[0 0.45 0.74]','faceColor','[0 0.45 0.74]');
ylim(ax(2), [0 90]);
st=(date(1));
se=(date(end));
% ax(1).XTickLabel = [st se];ax(2).XTickLabel = [st se];
% set(gca,'xtick');
set(ax(1),'xlim',[st se],'FontSize',12,'FontName', 'Times New Roman');
set(ax(2),'xlim',[st se],'FontSize',12,'FontName', 'Times New Roman');
% legend('show');
datetick('x','mmmyy','keeplimits');

 採用された回答

Rik
Rik 2019 年 5 月 8 日

0 投票

I don't know if there is a control for the non-marked ticks, but maybe box does something you like.
For the ticks, you should be able to use datetick to get there ( 'mm yy' as format should work).

8 件のコメント

Rt Ro
Rt Ro 2019 年 5 月 8 日
thank you for your reply. the problem of x-axis wasn't solved with your suggestion and all dates aren't displayed. could you please let me know if you have another idea?
Rik
Rik 2019 年 5 月 8 日
What code did you try? Try to make a MWE so we can run your code without any other dependencies and can reproduce your issue.
Walter Roberson
Walter Roberson 2019 年 5 月 8 日
What form is your x values in? Are you using datetime() object or using serial date numbers and datetick() ?
ax = gca;
s = ax.XLim(1); e = ax.XLim(2);
t = s:calyears(1):e;
xtick(t);
Rt Ro
Rt Ro 2019 年 5 月 9 日
I am using date numbers and datetick. (I have 3 columns year, month and day).
Walter Roberson
Walter Roberson 2019 年 5 月 9 日
Unless you are using r2014a or earlier we recommend that you use datetime to convert those numeric date components into datetime objects. For R2014b and R2015a and R2015b there are still some tricks to get the axes labels right but from R2016a I think it was, it should automatically format the ticks given a datetime object such as the s:calyears(1):e that I proposed
Rt Ro
Rt Ro 2019 年 5 月 9 日
my current version is 2018a, this is my code:
Eforc=load('forc.txt');
P=Eforc(:,5);
Q=Eforc(:,4);
date = datenum(Eforc(366:end ,1:3));
[ax,h1,h2] = plotyy(date,Q(366:end),date,P(366:end),'plot', 'bar');
set(ax(2), 'Ydir', 'reverse','YTick',[0,10,20]);
ylim(ax(1), [0 3]);
set(h2,'EdgeColor','[0 0.45 0.74]','faceColor','[0 0.45 0.74]');
ylim(ax(2), [0 90]);
st=(date(1));
se=(date(end));
% ax(1).XTickLabel = [st se];ax(2).XTickLabel = [st se];
% set(gca,'xtick');
set(ax(1),'xlim',[st se],'FontSize',12,'FontName', 'Times New Roman');
set(ax(2),'xlim',[st se],'FontSize',12,'FontName', 'Times New Roman');
% legend('show');
datetick('x','mmmyy','keeplimits');
Walter Roberson
Walter Roberson 2019 年 5 月 9 日
With that data and your release then using datetime instead of datenum would be cleaner.
Rt Ro
Rt Ro 2019 年 5 月 9 日
thank you so much. I changed datetime and keepticks instead of keeplimits, then the problem was solved.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

質問済み:

2019 年 5 月 8 日

コメント済み:

2019 年 5 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by