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

[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!

 Risposta accettata

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 Commenti

Mitsu
Mitsu il 6 Giu 2020
Modificato: Mitsu il 6 Giu 2020
Thank you. I am afraid my question missed a lot of details... your code works perfectly, but when I try to apply it to a contourf (where X and Y form a meshgrid), I try this:
S_axis = seconds(X_axis);
contourf(S_axis, Y_axis, Z_axis, N)
xtickformat('hh:mm')
This returns the error: "Error using contourf - Input arguments must be numeric or objects which can be converted to double."
I also tried creating a separate variable for the xticks:
X_ticks = -10800:10:+10800;
X_ticks = seconds(X_ticks);
xticks(X_ticks);
xtickformat('hh:mm');
I obtain the following error when trying to use xticks: "Tick values must be a vector of increasing numeric values."
...and the following error if I don't convert it to a duration (comment second line): "Invalid numeric tick label format."
Do you have any advice on how to work around this?
Mitsu
Mitsu il 6 Giu 2020
Modificato: Mitsu il 6 Giu 2020
My workaround for now is the following, but I wonder if there is a simpler way:
xt = xticks;
% Output
% xt = -200 -150 -100 -50 0 50 100 150 200
xtl = {};
for i = 1:length(xt)
dur = xt(i);
if dur < 0
dur = datetime('00:00:00') - minutes(dur);
xtl{i} = strcat('-',datestr(dur,'HH:MM'));
elseif dur > 0
dur = datetime('00:00:00') + minutes(dur);
xtl{i} = strcat('+',datestr(dur,'HH:MM'));
elseif dur == 0
xtl{i} = '0';
end
end
% Output:
% xtl = {'-03:20'} {'-02:30'} {'-01:40'} {'-00:50'} {'0'} {'+00:50'} {'+01:40'} {'+02:30'} {'+03:20'}
xticklabels(xtl)
The duration array will not work as an argument to meshgrid or contour. Use the usual numeric arrays, then use the appropriate parts of my code to plot the x-axis (‘S_axis’ here).
Try this:
X = -10800:10:+10800;
Z_axis = rand(1001,numel(X));
[S_axis,Y_axis] = meshgrid(X, (0:1000));
figure
contour(S_axis, Y_axis, Z_axis)
xt = get(gca, 'XTick');
xtl = compose('%s', duration(seconds(xt), 'Format','hh:mm'));
set(gca, 'XTick',xt, 'XTickLabel',xtl)
It works when I run it (R2020a). You may need to tweak it to work with your contour call, however I believe it will work without modification.
But it takes forever. I did this to get it to run in a reasonable time:
X = -10800 : 1000 : +10800;
Ferran — I believe my way is simpler.
Image Analyst — I believe the contour is what takes forever. I have no idea what Ferran’s data or contour call are. I got the duration array to work, and produce the appropriate tick labels with reasonable efficiency.
.
Thank you! I will try your suggested approach as soon as I come home today and "report back"!
My pleasure!
My day is over at this point, so I will check back here after 10:00 UTC.
Mitsu
Mitsu il 10 Giu 2020
Modificato: Mitsu il 10 Giu 2020
Thank you, Star Strider!
Your suggestion works as fast as mine, and is way simpler. The only thing I cannot seem to do is add the plus sign for positive durations (I was playing with the suggested %+ suggested in the documentation for compose).
Whenever that is not necessary, I am going to opt for your approach! Cheers
Here's what it looks like:
As always, my pleasure!
Change the ‘xtl’ and set lines to:
xtln = compose('%s', duration(seconds(xt(xt<=0)), 'Format','hh:mm'));
xtlp = compose('+%s', duration(seconds(xt(xt>0)), 'Format','hh:mm'));
set(gca, 'XTick',xt, 'XTickLabel',[xtln xtlp])
and the ‘+’ signs magically appear!
Thank you. I feel dumb for not having thought of just splitting it! Cheers!
As always, my pleasure!
No worries! Cheers!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Performance in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by