How to remove period stamp in datetime plot ?
조회 수: 9 (최근 30일)
이전 댓글 표시
How do I remove the period stamp when a datetime vector is plotted ?
Example code:
close all
t=datetime('now'):hours(1):(datetime('now')+days(7));
P=1:(size(t,2));
plot(t,P)
xtickformat('eee HH:mm' )
ax = gca;
ax.XTick = t(1):days(1):t(end);
Plot with period shown in lower right corner, indicated by the red arrow.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/144929/image.png)
댓글 수: 0
채택된 답변
vik
2019년 1월 19일
You can manually replace the strings used on the ticks after setting the XTick property:
close all
t=datetime('now'):hours(1):(datetime('now')+days(7));
P=1:(size(t,2));
plot(t,P)
% xtickformat('eee HH:mm' )
ax = gca;
ax.XTick = t(1):days(1):t(end);
ax.XTickLabelMode = 'manual';
ax.XTickLabel = datestr(ax.XTick,'ddd HH:mm'); % see datestr formatOut
댓글 수: 2
Manuel S Mathew
2021년 5월 7일
편집: Manuel S Mathew
2021년 5월 7일
Hi,
Thanks for this solution. I tried the code and it works in removing the period stamp but now my x axis is offset by one minute as below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/610525/image.jpeg)
My code is:
t = datetime(2017,1,1,0,0,0, "Format","dd-MMM-uuuu HH:mm"):hours(1):datetime(2017,1,1,23,0,0, "Format","dd-MMM-uuuu HH:mm");
plot(t,Data.Jan,'b')
grid;
title('January');
set(gca,'FontSize',18)
set(gca,'FontName','Times New Roman')
ax1 = gca;
ax1.XTick = t(1):hours(6):datetime(2017,1,1,23,0,0, t(end);
ax1.XTickLabelMode = 'manual';
ax1.XTickLabel = datestr(ax1.XTick,'HH:mm'); % see datestr formatOut
ylim([0 1000]);
Could you please help me?
dpb
2021년 5월 7일
>> ax1.XTick = t(1):hours(6):datetime(2017,1,1,23,0,0, t(end);
ax1.XTick = t(1):hours(6):datetime(2017,1,1,23,0,0, t(end);
↑
Error: Invalid expression. When calling a function or indexing a variable,
use parentheses. Otherwise, check for mismatched delimiters.
Your code posted has syntax errors and so can't be what caused/generated the plot shown.
But, if I just plot something using the above-generated time vector and the solution I posted/suggested below, I get the expected hour strings from 0:00 to 0:00 at 6 hour intervals precisely.
plot(t,Data.Jan,'b')
hAx=gca;
hAx.XTickLabel=hAx.XTickLabel;
will solve the problem methinks.
If not, we'll have to see the exact, running code that cause the problem, but the above won't run with the syntax error so it can't be what you actually did/have.
추가 답변 (2개)
dpb
2019년 1월 19일
편집: dpb
2019년 1월 19일
This was a side subject of a thread some time back with the need to combine durations with times at which the event durations occurred to build a 2D representation of events in time over a period of some years...I went off and looked for it but never could find it to refer to and didn't remember the "how" solved the issue.
Messing with the properties of the DateTimeRuler and even looking at undocumented/hidden properties, there appears that TMW left no way for the user to modify this as there (sorta') is with the Exponent property for the NumericRuler; there's no equivalent and while the property is in the object it has no effect on datetime axes...so, I didn't have any real Answer to give...
But, it just came to me that if one manually wrote the tick labels, perhaps the internal logic would be overwritten and, as the above shows, it's so...
There's a little more direct way to achieve the result, however, if one is satisfied with the ticks as is...
hAx=gca; % get the axes handle
hAx.XTickLabel=hAx.XTickLabel; % overwrite the existing tick labels with present values
has the effect of "turning off" the common string the inbuilt logic uses.
One could, it seems, build this into a callback function for cases where one changes limits or the like.
Why TMW seems to think "Mama knows best!" on these niceties that nobody would ever think their chosen default display formats aren't always going to be that wanted is frustrating to have to find these workarounds. While they're kinda' entertaining exercises for Answers, it's a real productivity-killer for the actual end-user.
댓글 수: 0
Abby Skofield
2023년 9월 20일
편집: Adam Danz
2023년 10월 18일
Starting in R2023b, you can use the xsecondarylabel function to toggle the visiblity of the secondary label off.
t=datetime('now'):hours(1):(datetime('now')+days(7));
P=1:(size(t,2));
ax = axes;
plot(t,P)
xtickformat('eee HH:mm' )
ax.XTick = t(1):days(1):t(end);
xsecondarylabel('Visible','off')
You can also use these new functions to add custom secondary labels:
ysecondarylabel('USD, millions')
For more info and demos on this topic, see the Graphics and App Building blog article below
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!