4

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, 2019 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. May 18, 2019 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. May 18, 2019 at 13:28

2 Answers 2

7

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). May 18, 2019 at 13:35
  • @LarrySnyder610 Now there are no more ticks at the extra positions.
    – user121799
    May 18, 2019 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! May 18, 2019 at 13:44
2

x tick label as interval is just what you want, but the problem is with axis lines=middle the tick position 0 is ignored by default and no tick is generated, so there are only two tick intervals instead of three, and so the first label ("low") goes missing.

My solution is to use extra x ticks to supply the missing interval and label but keep the extra ticks from showing with the help of style.

\documentclass[margin=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

    \begin{tikzpicture}

        \begin{axis}[
            width=5in,
            height=3in,
            axis lines=middle,
            scaled ticks=false,
            xmin=0,
            xmax=3,
            x tick label as interval,
            xtick={1,2,3},
            xticklabels={high,med},
            % first label would be missing w/o the lines below
            extra x ticks={0.1, 0.9},
            extra x tick labels={low},
            extra x tick style={
                tick style={draw=none}
            },
        ]

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

        \end{axis}
    \end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

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