2

Currently I have the figure as shown below (generated with the code below it) As you can see, the origin is not marked with 2 as I would expect based on the tick labels I explicitly set. Why does pgfplots do this and how can I put arbitrary text there?

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\begin{tikzpicture}
    \begin{axis}[
            axis lines=center,
            axis line style={-},
            xtick={-2,0,2},
            xticklabels={1,2,3},
            xmin=-3.5,
            xmax=3.5,
            ymin=0,
            ymax=5,
        ]
        \addplot+[ycomb, mark=none] table {
            -3 1
            -2 2
            -1 3
            0 4
            1 3
            2 2
            3 1
        };
    \end{axis}
\end{tikzpicture}
\end{document}
gernot
  • 49,614
Octaviour
  • 353
  • 3
  • 14
  • 2
    Replace axis lines=center by axis y line=middle, axis x line=bottom. – gernot Jan 03 '17 at 11:53
  • @gernot I think this is not a duplicate, see Stefan's answer. pgfplots trying to avoid overdrawing over tick label – percusse Jan 03 '17 at 12:06
  • 1
    @percusse As I understand the other post, pgfplots thinks (because of axis lines=center) that the y axis extends below the x axis and therefore obscures the tick. In this case we now have two cures for the same disease, where hide obscured x ticks=false seems to cure only the symptom. – gernot Jan 03 '17 at 12:18
  • @gernot Ah yes, I read too fast. You are right – percusse Jan 03 '17 at 12:54

1 Answers1

3

This is due to the default setting of hide obscured x ticks which is true. Set it to false and you will get the desired result.

But the better solution is the one given by gernot in his comment below the question and as it has been voted for the duplicate question.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
                axis lines=center,
                axis line style={-},
                xtick={-2,0,2},
                xticklabels={1,2,3},
                hide obscured x ticks=false,    % <-- added
                xmin=-3.5,
                xmax=3.5,
                ymin=0,
                ymax=5,
            ]
            \addplot+[ycomb, mark=none] table {
                -3 1
                -2 2
                -1 3
                0 4
                1 3
                2 2
                3 1
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535