2

Is it possible to tell pgfplots to put tick labels between the tick marks instead of at the tick marks?

MWE:

\documentclass[margin=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

    \begin{tikzpicture}

        \begin{axis}[
            width=5in,
            height=3in,
            axis lines=middle,
            scaled ticks=false,
            xtick=\empty,
            xmin=0,
            xmax=3,
            extra x ticks={0,1,2,3},
            extra x tick labels={low,high,med},
        ]

        \addplot [mark=*,draw] coordinates {(0.5,2) (1.5,4) (2.5,3)};

        \end{axis}
    \end{tikzpicture}

\end{document}

Result:

enter image description here

I can do it manually by adding

xticklabel style = {xshift=1.9cm,align=center},

enter image description here

but this seems unsatisfying, unreliable, and requires too much trial and error.

3
  • Why not use extra x ticks={0.5,1.5,2.5},? – leandriis May 18 '19 at 13:25
  • Because then the tick marks will appear at 0.5, 1.5, 2.5, which I don't want -- I want the marks at the integers. – LarrySnyder610 May 18 '19 at 13:27
  • The image is meant to suggest "in between 0 and 1, things are low; in between 1 and 2, things are high; in between 2 and 3, things are medium. – LarrySnyder610 May 18 '19 at 13:28
3

How about

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}

    \begin{tikzpicture}

        \begin{axis}[
            width=5in,
            height=3in,
            axis lines=middle,
            scaled ticks=false,
            xtick=\empty,
            xmin=0,
            xmax=3,
            xtick={0,1,2,3},
            xticklabel=\empty,
            extra x ticks={0.5,1.5,2.5},
            extra x tick labels={low,high,med},
            extra x tick style={major tick length=0pt}
        ]

        \addplot [mark=*,draw] coordinates {(0.5,2) (1.5,4) (2.5,3)};

        \end{axis}
    \end{tikzpicture}

\end{document}

enter image description here

3
  • Nice, but I don't want the tick marks at the labels -- I want them at the integers (in this MWE). – LarrySnyder610 May 18 '19 at 13:35
  • @LarrySnyder610 Now there are no more ticks at the extra positions. – user121799 May 18 '19 at 13:36
  • OK, that works. It's still a little clunky IMO because there's a lot of manual handling (i.e., I'm still basically doing the x-shifting manually). In my actual figure, there are a lot more intervals on the x-axis, so it's a bit tedious, but workable. I was hoping there was just an option like labels between ticks. Anyway, I'll keep my eye out for a more automated solution. Thanks! – LarrySnyder610 May 18 '19 at 13:44

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.