Plotting dates as text in the x-axis

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 Kommentare

Adam Danz
Adam Danz am 7 Jun. 2019
" I want to plot them without order."
What does that mean? The x axis is always in ascending or descending order. Could you give an example?
Tarek Zaqout
Tarek Zaqout am 7 Jun. 2019
I tried to convert the datetime data to string and plot them as they are with their corresponding values.
example:
21-Feb-2019 09:00:00 5.6
23-Jan-2019 13:00:00 4.5
and so on.
Adam Danz
Adam Danz am 7 Jun. 2019
Bearbeitet: Adam Danz am 7 Jun. 2019
If your data are a timeseries you shouldn't need to convert the datetime data to strings.
Tarek Zaqout
Tarek Zaqout am 7 Jun. 2019
Well I don't necessarly need to convert them, but like you said they will be shown either ascending or descending. I want to plot them as they are so I figured if they are text data they can be plotted as names. Is there a way to do so.
Thanks
Adam Danz
Adam Danz am 7 Jun. 2019
I think I understand what you're asking now. Your timetable is not in chronological order and you'd like to create a plot in the order that your data are stored in the time table. Is this correct?
Tarek Zaqout
Tarek Zaqout am 7 Jun. 2019
Yes exactly. I'm trying to plot rainfall events ranking from highest to lowest with their corresponding dates.
dpb
dpb am 7 Jun. 2019
See additional comment to Answer...hadn't seen this comment when wrote it--dpb
How many events do you have to try to show on a single figure? For this purpose maybe bar would be better choice than plot? If not too many, could then possibly write the date in the bar for each -- but you may still have real estate issues depending on how many events you are trying to show.
Tarek Zaqout
Tarek Zaqout am 7 Jun. 2019
I thought so. No actually they are too many. They are for a time period extending for 4 months. Hourly data.
dpb
dpb am 7 Jun. 2019
Bearbeitet: dpb am 7 Jun. 2019
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

dpb
dpb am 7 Jun. 2019
Bearbeitet: dpb am 7 Jun. 2019

1 Stimme

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 Kommentare

Tarek Zaqout
Tarek Zaqout am 7 Jun. 2019
Well, I'm trying to plot the biggest rainfall events. I have ranked them from largest to smallest, and I want to plot them this way. The problem is that I can't find a way to plot them with their respective dates without plotting them in an ascending manner (e.g. Feb 1st to Feb 28th). Instead I want them to be plotted descending from highest event to lowest.
I thought if I there's a way for matlab to interpret the dates as names and plot accordingly. It seems not possible though.
dpb
dpb am 7 Jun. 2019
Bearbeitet: dpb am 7 Jun. 2019
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Gefragt:

am 7 Jun. 2019

Bearbeitet:

dpb
am 7 Jun. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by