How to change only the last xticklabel?

You can assign names to the label nodes and access the last one through that. The node text can't be changed after it has been created, unfortunately, so you will have to paint over it with the changed label. Here's a new style called overwrite last x tick label that takes an optional argument to specify what the last label should be overwritten with. The style first draws a white rectangle over the old label and then puts a new node on top of that. That ensures that the old label is completely covered, even if the new label takes up less space.

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{
    overwrite last x tick label/.style={
        every x tick label/.append style={alias=lasttick},
        extra description/.append code={
            \fill [white] (lasttick.north west) ++(0pt,-\pgflinewidth) rectangle (lasttick.south east);         
            \node [anchor=base] at (lasttick.base) {#1};}
    },
    overwrite last x tick label/.default={$\infty$}
}

  \begin{tikzpicture}
    \begin{axis}[overwrite last x tick label]
      \addplot table [row sep=crcr]{
      0 1\\
      2 1\\
      3 2\\
      4 3\\};
    \end{axis}
  \end{tikzpicture}
\end{document}

You can use the axis description coordinate system:

enter image description here

Notes:

  • Needed to add clip=false to the axis.
  • The fill=whites is so that the real label is hidden below the \infty.
  • draw=none can be omitted.
  • yshift=1.5ex was necessary to get the label in the correct vertical position. Perhaps there is a better way to do this.
  • If you explicitly specify xmax in the axis options you can access this value thru \pgfkeysvalueof{/pgfplots/xmin} as per How to access \xmin, \xmax, \ymin, \ymax from within PGFplots axis environment.

Code:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture} 
\begin{axis}[clip=false] 

\addplot {x}; 

\node[draw=none,yshift=1.5ex,fill=white] at (xticklabel cs:1) {$\infty$};
\end{axis} 
\end{tikzpicture}
\end{document}

You can use TeX (or LaTeX) conditionals based on \tick pt as long as the numbers are within the LaTeX number range -16384...16384. Here, the suffix pt allows you to apply fixed point arithmetics (because pt are represented as fixed point numbers in TeX):

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
      xticklabel={\ifdim\tick pt<6pt $\pgfmathprintnumber{\tick}$ \else $\infty$\fi}
    ]
      \addplot+[] {x};
    \end{axis}
  \end{tikzpicture}
\end{document}

Here, I chose "6" as the right-most value.

Clearly, the approach needs manual tuning because it assumes that there is exactly one tick with position >= 6.

Tags:

Pgfplots