plot with two datetick axis

6 ビュー (過去 30 日間)
Randall
Randall 2019 年 7 月 3 日
編集済み: Adam Danz 2019 年 7 月 8 日
I want to plot x,y data where the x axis is a datenum. And I want to display the x-axis details in both UTC time and local time, which is 6 hours later. Datetick seems to want to create tick marks at "good" places for time so humans can make better sense of it. But having both time zones represented means the data seems to be shifted 6 hours.
So, is it possible to have two x-axis of tick marks and labels each of which is for the same x,y data but one is shifted 6 hours.
Thanks,
RAndall

回答 (2 件)

dpb
dpb 2019 年 7 月 3 日
Don't use datenum and datetick, use datetime instead for which plot has builtin support.
You should be able to create two independent x-axes but plot against only the one--it's unfortunate TMW didn't complete the task when they built yyaxis with its twin xxaxis that would make things more user-friendly.

Adam Danz
Adam Danz 2019 年 7 月 3 日
編集済み: Adam Danz 2019 年 7 月 8 日
dpb's solution is a good one. You'll need to make sure the upper and lower tick marks align (use 'XTick' property and xlim).
Here's an alternative that puts a 2nd line of xlabels along the bottom axis. Since TMW doesn't allow multiple lines in the axis labels, this replaces the XTickLabel with text objects.
% Create figure
figure
t = hours(5):minutes(30):hours(8);
y = rand(1,7);
plot(t,y);
xtickformat('hh:mm')
axh = gca();
% Set x tick and x lim.
% It's important theses are set and not 'auto'
xlim([min(t), max(t)])
axh.XTick = t;
% compute time in other time zone
tShift = t - hours(3); %assuming 3 hr difference
% Create the tick labels
ticktxt = join([string(datestr(t,'hh:MM')),repmat(newline,numel(t),1),datestr(tShift,'hh:MM')]);
% add new tick labels as text objects
axh.XTickLabel = [' '];
text(axh.XTick, min(ylim(axh))*ones(size(axh.XTick)), ticktxt, ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'Top');
% add axis lables
xlh = xlabel('Local time / UHC (-3)'); %bottom
xlh.Position(2) = xlh.Position(2)-.008; %move the xlable down a bit
  2 件のコメント
dpb
dpb 2019 年 7 月 8 日
Out of curiosity, there seems to be a slight alignment issue on the lower of the two??? Is there an extra space in there or something, maybe??
Adam Danz
Adam Danz 2019 年 7 月 8 日
編集済み: Adam Danz 2019 年 7 月 8 日
I noticed that too. The following line produces the x tick labels.
% Create the tick labels
ticktxt = join([string(datestr(t,'hh:MM')),repmat(newline,numel(t),1),datestr(tShift,'hh:MM')]);
the string() command does not add extra space.
datestr(t,'hh:MM')
ans =
7×5 char array
'05:00'
'05:30'
'06:00'
'06:30'
'07:00'
'07:30'
'08:00'
It appears that the newline() command may be adding space before and after the char(10) (newline) character. Note the space before and after the return carriage.
ticktxt =
7×1 string array
"05:00 ↵ 02:00"
"05:30 ↵ 02:30"
"06:00 ↵ 03:00"
"06:30 ↵ 03:30"
"07:00 ↵ 04:00"
"07:30 ↵ 04:30"
"08:00 ↵ 05:00"
size(ticktxt{1})
ans =
1 13
% size would be 11 otherwise
ADDENDUM
The space is added with strings (" ") but not characters (' ').
t = join(["1",newline(),"2"])
t =
"1
2"
t = join(['1',newline(),'2'])
t =
'1
2'

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

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by