# Dates as xticklabels ; number formated dates provided

I would like to plot temporal data and have dates as tick labels. I have seen chapter 4.20 Symbolic Coordinates and User Transformations from pgfplots manual but it does not correspond to my situation because my dates are already formated as numbers. There is a remark to that effect

Remark: This section applies to users who want to have non-standard input coordinates. If you have normal numbers which don't need to be transformed and you like to have special symbols as tick labels, you should consider using the xticklabels (yticklabels) key described on page 224.

So I have read page 224 and following but I could not figure it out.

I reckon something could be done with the pgfplotstable and pgfcalendar packages but once again I fail to find the relevant information.

I ended up using a manual solution which require adjustment whenever the data change and even needed a scaled ticks=false to prevent some overlapping in some specific cases.

How could I have done this cleanly ?

Here is an exemple derived from my current code :

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\begin{tikzpicture}
7.3124563e+005 4
7.3139367e+005 3
7.3165463e+005 2
7.3233467e+005 1
7.3262672e+005 6
7.3279571e+005 5
7.3320909e+005 4
7.3338963e+005 3
7.3385267e+005 2
7.3465509e+005 6
}\mytable

\begin{axis}[%
xtick={730852, 731582, 732313, 733043, 733774, 734504},%ugliness here
xticklabels={2001, 2003, 2005, 2007, 2009, 2011},%and there
xlabel={Date},
ylabel={Data},
scale only axis,
width=8cm,
height=5cm,
axis on top,
scaled ticks=false]%and here again!

\end{axis}
\end{tikzpicture}

\end{document}


The time format I use comes from MATLAB datenum and datestr function which is similar to what is used by the pgfcalendar package except that a shift in range is needed. Namely, one unit is one day, fractional part gives the time. 1-Jan-0000 is coded by 0.0 and 7.3188e+005 correspond to 24-Oct-2003 12:45:07.

-
 Can you also include the relation between the numbers and the years? I can't see how one can obtain the years from that. – percusse May 25 '12 at 16:32 They are number of days ↷ see edit. – Alfred M. May 25 '12 at 21:54 It can be done with x filter/.code, but in my humble opinion, you can just use excel or matlab to convert them automatically since it won't be worth it to do a full rigorous conversion. Is there any limitation on your side that prevents this? – percusse May 26 '12 at 9:24 Numbers are easier to handle and store in files that I pass from matlab to R or other software. I was wondering if it could be digested by pgfplots as is. – Alfred M. May 26 '12 at 12:10 You seem to generated tables anyway. The most simple solution (which might still be an answer, although in an undesired direction) seems to be to reconfigure your scripts to generate an additional columns in the format pgfplots expects - and ignore that one in R. Aside from that, you could try to run the INVERSE datatime transformation of pgfplots on your data before the dateplot library processes it (requires detailed knowledge of pgfkeys and the x filter stuff of pgfplots). Let us know if you decide to stick with the filter-solution. – Christian Feuersänger May 27 '12 at 7:38
show 1 more comment

I thought it might be a simple thing to modify the coordinate transformation to fit your needs.

It turned out to have the following unexpected side-effects:

1. precision. The dateplot lib of pgfplots uses integer arithmetics to shift to date ZERO. This is lost for your application unless someone implements this "properly".
2. changing the trafo will require any user input (like xmin or xtick) in numeric format unless someone implements a solution which accepts both numeric and textual date formats.
3. matlabs output is NOT the same as pgfcalendar expects. One needs to shift the number range. This needs to be done in integer arithmetics, otherwise the limited precision in TeX will lead to a severe degradation of the precision.

As a consequence, I strongly suggest to reconfigure your application to export properly formatted text dates as third column and configure pgfplots to read it together with its date coordinates=x style.

You can post a feature request to implement support for the three mentioned problems above.

I have a running prototype which ignores (reveals) all these problems. If you still want to see it, you can contact me by mail. I will not post it here because it might produce unexpectedly bad results (or perhaps not so unexpected at all).

-
 I appreciate your precise answer very much. Somehow it reassures me for I was rather baffled by what I had tried and was wondering if I was able to read a manual! I am interested to see your code. I think I lack the skills to do anything about it though. Were can feature requests be posted? – Alfred M. May 28 '12 at 8:13 To post feature requests, you can use sourceforge.net/projects/pgfplots (then trackers-feature requests). Do not expect rapid progress, though - I am author of pgfplots and would have implemented it right-away if it was as simple as expected in the first place (it is not that difficult; there are just more open tickets...) Please extract my contact data from the pgfplots manual, and I will send you what I got so far. – Christian Feuersänger May 28 '12 at 15:08 Ah, by the way: if you submit feature requests, you could simplify the process if you also include a way to convert between matlab's numeric date format and the one of pgfcalendar (i.e. the shift in full days). – Christian Feuersänger May 28 '12 at 15:10