6

Rather than adding a second x axis by putting it on the other side of the plot I would like to add one implicitly simply by adding another row of tick labels to an existing axis. What I am after is something like the following mockup:

Mockup of a plot with an extra row of x axis tick labels

Using the idea in this answer I have tried to come up with something like this but there are some problems and maybe there is a better approach to the issue?

My failed attempt of reproducing the mockup

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{
      domain=0:1,
      xmin=0, xmax=1,
      ymax=1, ymin=0
    }

\begin{document}

\begin{figure}
  \begin{tikzpicture}
    \begin{axis}[xtick align=inside,
      extra x ticks={0.2, 0.4, ..., 0.8},
      extra x tick labels={0.33, 0.33, 0.33, 0.33},
      every extra x tick/.style={
        xtick align=outside,
      },
      xlabel=\(x_1\),
      after end axis/.code={
        \node  at (rel axis cs:1,0) [anchor=south west, align=left] {\(x_2\)};
      }
      ]
      \addplot{x};
    \end{axis}
  \end{tikzpicture}
\end{figure}

\end{document}
N.N.
  • 36,163

1 Answers1

7

Using major tick length=0pt for the every extra x tick style, you can get rid of the ticks for the extra labels; you can prevent the overlapping using a yshift; using the axis description coordinate system you can place \(x_1\) and \(x_2\):

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{
      domain=0:1,
      xmin=0, xmax=1,
      ymax=1, ymin=0
    }

\begin{document}

\begin{figure}
  \begin{tikzpicture}
    \begin{axis}[xtick align=inside,clip=false,
      extra x ticks={0, 0.2, 0.4, ..., 1},
      extra x tick labels={0.33, 0.33, 0.33, 0.33, 0.33,0.33},
      every extra x tick/.style={major tick length=0pt,
        xtick align=outside,yshift=-10pt}
      ]
      \addplot{x};
        \node  at (axis description cs:0,0) [anchor=north east, align=left,xshift=-12pt] {\(x_1\)};
        \node  at (axis description cs:0,0) [anchor=north east, align=left,xshift=-12pt,yshift=-11pt] {\(x_2\)};
    \end{axis}

  \end{tikzpicture}
\end{figure}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • Thanks this does what I want for single plots. However when I use it with groupplots the position of x1 and x2 is not right. – N.N. Aug 06 '12 at 15:02
  • @N.N. please add to your question a minimal example. – Gonzalo Medina Aug 06 '12 at 15:06
  • I was wrong in what caused it. You can reproduce it by setting the plotted function to another, e.g. change to \addplot{x - 0.5}; and you can see that x1 and x2 are displaced. – N.N. Aug 06 '12 at 15:17
  • @N.N. I've updated my answer; using the axis description coordinate system should fix the problem. – Gonzalo Medina Aug 06 '12 at 15:40
  • Cheers! Is it possible to get it go work without clip=false? Without clip some functions, e.g. \addplot{ln(1 / x)};, grows outside the axis. A fix might be to use restrict y to domain=-1:1, but I dunno if it is the right way to go. – N.N. Aug 06 '12 at 16:00
  • @N.N. since the labels x_1, x_2 should appear outside the box, I think that using clip=false and restrict y to domain (as you suggest) is the way to go. – Gonzalo Medina Aug 06 '12 at 16:34
  • In the manual in §2.6.2 under the heading Dimension Too Large Errors it says "Consider using the restrict x to domain*= min : max key in such a case, where the min and max should be (say) four times of your axis limits (see page 272 for details)." I dunno if this is a warning of using restrict x/y/z to domain with a value equal to the axis limits. – N.N. Aug 06 '12 at 17:19
  • @N.N. that remark addresses the case in which you get an error of the "Dimension too large" kind due to viewing a very small portion of the data range. The use of restrict x/y/z to domain can prevent the error in those cases. I don't see any warning about the use of the option with sensible values. – Gonzalo Medina Aug 06 '12 at 17:29