# How can I use two x axes in pgfplots?

I would like to draw two x axes with ticks and legends in the bottom and top axes where one axis is a linear transformation of the other for the same y range. There is a similar application described in section 4.8.10 of the pgfplots manual for two y axes. Are two x axes possible using the axis environment?

• Have you tried adopting the example to use two x-axes? If so, what didn‘t work? – Psirus Jan 13 '12 at 16:39
• Using the same example substituting x by y, two axes appear in the upper one. – Miguel de Val-Borro Jan 13 '12 at 17:57
• Some good examples that you might find interesting are shown in this post: tex.stackexchange.com/questions/27451/… – agodemar Jan 14 '12 at 14:34
• What is the matter with the given answer? – Psirus Jan 16 '12 at 21:34

## 1 Answer

As a default, both axes are plotted left/right & top/bottom. That’s why you get both ticks on the upper axis (I assume that is what you meant). You need to explicitly tell pgfplots you don’t want the upper part of the x-axis in the first {axis} by supplying axis x line*=bottom. Anyway, I find this really irritating to look at. Unless you supply additional information to the plot, there is no way of distinguishing which curve belongs to which axis, making the plot useless. Maybe in your application there is less chance of confusion, but still, beware.

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
scale only axis,
xmin=-5,xmax=5,
axis y line*=left,
axis x line*=bottom]
\addplot {x^2};
\end{axis}
\begin{axis}[
scale only axis,
xmin=0,xmax=15,
domain=0:15,
axis y line*=right,
axis x line*=top]
\addplot[red] {x};
\end{axis}
\end{tikzpicture}
\end{document} • This is what I need, thanks. I have used the code without scale only axis. – Miguel de Val-Borro Jan 14 '12 at 11:56
• @MigueldeVal-Borro Reversed your edit. You are of course free to edit an answer, but it didn’t really add any new information. We try to keep the answers concise. – Psirus Jan 14 '12 at 14:17