2

Somehow I am struggling with the ticklabels of my plot. Here is my mwe:

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\begin{document}
\begin{figure}
\begin{tikzpicture}
 \begin{axis}[scaled ticks=false,
              restrict x to domain=0:10,
              xtick distance=1,              
              ytick distance=500,
              restrict y to domain=0:3500,
              ylabel = {test},
              legend pos= {north west}
              ]
   \addplot[no marks, blue] table [x=a, y=b] {data/testdata.txt};
   \addlegendentry{test}
 \end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

The dataset:

a   b
1   370
2   740
3   1110
4   1480
5   1850
6   2220
7   2590
8   2960
9   3330

This produces the following:

enter image description here

Now I want to replace the yticklabel 3000 with the unit so I added the following line after ytick distance:

yticklabels={0, 500, 1000, ..., 2500, unit, 3500},

But now the y domain starts at 500 and ends at 4000 and the labels 0 and 500 are missing. What am I doing wrong? Whats the best way to replace just one ticklabel?

enter image description here

sporc
  • 643

2 Answers2

2

PGFPlots is creating two invisible tick marks in the plot, so when you add yticklabels={0, 500, 1000, ..., 2500, unit, 3500}, the first two are consumed to fill that gap.

You can enforce the desired ytick to make it work properly with: ytick={0, 500, ..., 3500}.

enter image description here

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{testdata.txt}
a   b
1   370
2   740
3   1110
4   1480
5   1850
6   2220
7   2590
8   2960
9   3330
\end{filecontents}
\pgfplotsset{compat=1.5}
\begin{document}
\begin{tikzpicture}
 \begin{axis}[scaled ticks=false,
              restrict x to domain=0:10,
              xtick distance=1,
              ytick distance=500,
              ytick=      {0, 500, ..., 3500},
              yticklabels={0, 500, ..., 2500, unit, 3500},
              restrict y to domain=0:3500,
              ylabel = {test},
              legend pos= {north west}
              ]
   \addplot[no marks, blue] table [x=a, y=b] {testdata.txt};
   \addlegendentry{test}
 \end{axis}
\end{tikzpicture}
\end{document}
2

Your approach to place the unit would work, but in case your ticks change you would have to do a lot of work to fix it again. Thus, I suggest to modify the answer given at https://tex.stackexchange.com/a/416810/95441 for a general approach.

(In addition you use some axis options that are not relevant for your code, i.e. removing them gives the same result. I therefore removed them.)

For details please have a look at the comments in the code.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.5,
        % adjusted from <https://tex.stackexchange.com/a/416810/95441>
        din yunit2/.style={
            yticklabel style={
                name=ylabel\ticknum,
                append after command=\pgfextra{\xdef\lastyticknum{\ticknum}}
            },
            % changed `.code' to `.append code'
            after end axis/.append code={
                \pgfmathparse{int(\lastyticknum-1)}
                % first "overdraw" the node by rectangle, so it isn't seen any more
                \fill [white]
                    (ylabel\pgfmathresult.south west)
                        rectangle
                    % the `xshift' is needed to not overdraw the axis line
                    ([xshift=-0.4pt]ylabel\pgfmathresult.north east);
                % then place the "unit" text
                \node [anchor=base] at (ylabel\pgfmathresult.base) {#1};
            },
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xtick distance=1,
        ytick distance=500,
        ylabel={test},
        legend pos={north west},
        din yunit2={\si{\meter}},
    ]
        \addplot [no marks, blue] table [x=a, y=b] {
            a   b
            1   370
            2   740
            3   1110
            4   1480
            5   1850
            6   2220
            7   2590
            8   2960
            9   3330
        };
        \legend{test}
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535