14

I have data that is measured in a seconds scale. If I plot the data using pgfplots, the automatic tick labes are multiples of 100. Because the axis is "time", I would like to be the ticks multiples of 60 (or maybe 120 if too large).

I know, I can manually define this using xtick={0,60,...,420}, but that requires me to know the size of the data sample. As I have many samples that also may change in the future I would like to do this automatically.

My question is: How do I change the automatic tick distance to a different value?

\documentclass{standalone}
\usepackage{amsmath,tikz,pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[xtick={0,60,...,420}]
      \addplot coordinates {
        (0,0)
        (90,0)
        (110,10)
        (150,60)
        (170,70)
        (220,90)
        (300,120)
        (430,150)
      };
    \end{axis}
  \end{tikzpicture}%
\end{document}

2 Answers 2

8

This is where the x coord trafo and x coord inv trafo mechanisms come in handy.

Setting x coord trafo/.code={\pgfmathparse{\pgfmathresult/60}} transforms your data from seconds to minutes on the fly, so the tick labels will be placed every minute (or at integer multiples of minutes, depending on the available space).

To display the tick labels in seconds, you set x coord inv trafo/.code={\pgfmathparse{\pgfmathresult*60}}. This undoes the initial transformation, but only for the tick labels: the positions of the tick marks remains unchanged.

\documentclass{article}
\usepackage{amsmath,tikz,pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
            width=12cm, height=7cm,
            x coord trafo/.code={
                \pgfmathparse{\pgfmathresult/60}
            },
            x coord inv trafo/.code={
                \pgfmathparse{\pgfmathresult*60}
            }
        ]
      \addplot coordinates {
        (0,0)
        (90,0)
        (110,10)
        (150,60)
        (170,70)
        (220,90)
        (300,120)
        (430,150)
      };
    \end{axis}
  \end{tikzpicture}%
\end{document}
1
  • Thanks, that is incredibly awesome because it still uses the automatic algorithm to compute the "right" tick distance if range becomes larger.
    – kap
    Commented Sep 5, 2014 at 13:09
22

You can change the automatic tick distance with the key xtick distance which is available in PGFPlots v1.13. See section 4.15.1 on page 311 of the manual.

\documentclass{standalone}
\usepackage{amsmath,tikz,pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      xtick distance={60},
    ]
      \addplot coordinates {
        (0,0)
        (90,0)
        (110,10)
        (150,60)
        (170,70)
        (220,90)
        (300,120)
        (430,150)
      };
    \end{axis}
  \end{tikzpicture}%
\end{document}

image of above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .