0

I have many graph using date as x axis, and I wonder if there is an option of xtick to put a tick every hour automatically and not writting every tick like that :

xtick = {
{2018-04-27 05:00},
{2018-04-27 06:00},
{2018-04-27 07:00},
{2018-04-27 08:00},
{2018-04-27 09:00},
{2018-04-27 10:00},
{2018-04-27 11:00},
{2018-04-27 12:00},
{2018-04-27 13:00},
{2018-04-27 14:00},
{2018-04-27 15:00},
{2018-04-27 16:00},
{2018-04-27 17:00},
{2018-04-27 18:00},
{2018-04-27 19:00},
{2018-04-27 20:00}
}

Cause I've tried this, that didn't worked and that I know works for years alone :

xtick = {
{2018-04-27 05:00},
{2018-04-27 06:00},
...,
{2018-04-27 20:00}
}

Here is an exemple of what I would like to get in the end :

!https://imgur.com/frxM0h2

Thank you !

  • Where do the X and Y values come from? If it is from a file, then tex.stackexchange.com/questions/267452/… might be interesting, otherwise maybe tex.stackexchange.com/questions/133857/…. Can you make a complete MWE that shows a plot with the manual list of xticks? That will help in understanding the problem and finding a solution. – Marijn Jul 27 '18 at 11:30
  • If you need to cover more than one day, you might use the Unix time which is just a number of seconds since specified time therefore easy to increment. Then you can convert it to more readable format using methods from this queation: tex.stackexchange.com/a/147707/131547 – SPQR211 Jul 27 '18 at 11:36
  • @Marijn Well I added a picture, but the link didn't showed it :( – Hugo Vernier-Lambert Jul 27 '18 at 12:09
  • @SPQR211 I'm gonna look at this and come back if needed – Hugo Vernier-Lambert Jul 27 '18 at 12:11
3

If only the hour changes, you might want to consider using only the hour as a ticklabel.


If you definitely want the entire time/date, you can use the xticklabel option as follows:

\documentclass[tikz,margin=2mm]{standalone}

\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xticklabel = {%
                \pgfmathparse{int(\tick)<10?"0":{},int(\tick)}%
                2018-04-27 \pgfmathresult:00%
            },
            xticklabel style={rotate=90,anchor=east},
            xtick distance=1,
            xmin = 5,
            xmax = 20,
        ]
            \addplot+[domain=5:20] ({x},{x^2});   
        \end{axis}
    \end{tikzpicture}
\end{document}

I used this answer to add a leading zero to the hour.

Result:

enter image description here

  • When i try your xticklabel, I get something weird showing : "2018-04-27 1987:00" at each tick : imgur.com/frxM0h2 – Hugo Vernier-Lambert Jul 27 '18 at 12:15
  • I found a way ! The xtick distance that you use is enough to make it, just by dividing 1 (day) per 24 (number of hours) and it works ! Thank you for your interest in the question – Hugo Vernier-Lambert Jul 27 '18 at 12:28
  • @HugoVernier-Lambert I don't see what you mean in the image you attached, and I don't really understand what you mean with your fix, but I am glad that I could be of help. – Max Jul 27 '18 at 12:42
  • imgur.com/2NrBojh I've put a wrong image link sorry, here is the one showing the probleme – Hugo Vernier-Lambert Jul 27 '18 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.