Plotting dates as text in the x-axis
3 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a timeseries in the form of '23-Feb-2019 05:00:00' and I would like to plot it them as text in the x-axis. The problem I'm having is that I want to plot them without order. Either as text or as datetime data. They're originally datetime and when I plot them, Matlab puts them in order.
Thanks.
9 个评论
dpb
2019-6-7
编辑:dpb
2019-6-7
Ouch! 4*30*24 = 2880. There's no way to even show that many distinct events on a plot--a typical monitor resolution is somethng like 1920x1080 pixels so it's about 1.5:1 events/pixel even if the line took up the entire monitor--and a typical figure display is only using a small fraction of the screen real estate.
回答(1 个)
dpb
2019-6-7
编辑:dpb
2019-6-7
To plot as ordinal position in the array instead of in order (what else could plot do with actual values on an axis with numerical or temporal variables but plot the data where it belongs?) use the one-argument form for plot as
hL=plot(Y);
We don't have enough context to know whether by "timeseries" you mean a MATLAB timeseries class or just a set of data with associated times in some other storage scheme so can't write a specific solution that know applies to your case exactly as far as syntax. Y is your data above without the time--use whatever variable name you have and what form is needed to reference it as it is stored.
Then use the 'XTickLabel' property to write the dates to the axis. Probably the best would be to start by retrieving the 'XTick' values and building a datetime array from them by interpolating/extrapolating from the input data times by position index for "pretty" intervals or set xlim([1 numel(Y)]) to match the exact number of elements in the time series to the axes, then retrive the timestamps at the 'XTick' values and write to 'XTickLabel'. Probably will have to do some additional clean up formatting once you see how much room you've got--time labels are way builkier than just numeric labels; may have room for only every other or even every 3rd or 4th...
ADDENDUM
On reflection, this seems to have a general flaw in that if that data aren't in sequential order and you label some subset of points there's no way to know what time the other points represent--as described, they could be anything?
What is the point of plotting time-specific data on some other sequential order when won't be able to identify which are associated with what time for all simply for lack of there being sufficient space to do so? What are we trying to illustrate with this particular plot--seems like some other format would likely be better for the purpose.
2 个评论
dpb
2019-6-7
编辑:dpb
2019-6-7
The above is the way to plot in the sorted/ordinal order by ranking. It totally defies the meaning of a numerically-ordered axes for the dates to be plotted as the abscissa value and not be placed on the axis at their proper location--if that were to happen, it would break every other application of such an axis.
You could make the dates a categorical variable but by default they would be sorted so you would have to specify the associated order vector...I hadn't thought of that option when writing the above. But, see the above note regarding the number of events you're trying to show and physical limitations of display hardware.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!