How to show a seconds plot axis ticks as hh:mm

11 ビュー (過去 30 日間)
Mitsu
Mitsu 2020 年 6 月 6 日
コメント済み: Star Strider 2020 年 6 月 10 日
[Edited to clarify that X is a 2x2, part of a meshgrid.]
Assume the data for the x-axis is duration in seconds, in both directions, with values ranging from -10800 to +-10800 seconds.
I use a contourf(X,Y,Z) to plot it.
In the x-axis, instead of:
-10800, -9000, ... , 9000, 10800
I would like to show the following ticks:
-03:00, -02:30, ... , +02:30, +03:00
That is:
  • Change seconds duration to hh:mm
  • Keep negative sign in front of the negative durations
  • Add the plus sign to the positive values (I could do this via xtickformat('%+4.4g');, but I am not able to combine this with the hh:mm format)
I have been playing around with xtickformat and datetick, but had no success so far.
Thank you!

採用された回答

Star Strider
Star Strider 2020 年 6 月 6 日
Try this:
X = -10800:10:+10800;
tv = seconds(X);
signal = sin(2*pi*X/1E+3); % Create Signal
figure
plot(tv, signal, '-')
xtickformat('hh:mm')
I created ‘signal’ to test the code. This works as desired in R2020a.
.
  11 件のコメント
Mitsu
Mitsu 2020 年 6 月 10 日
Thank you. I feel dumb for not having thought of just splitting it! Cheers!
Star Strider
Star Strider 2020 年 6 月 10 日
As always, my pleasure!
No worries! Cheers!

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

その他の回答 (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