2

In PGFPlots, I'd like to set the default axis description location near the arrow for non boxed axes and otherwise keep the existing default of placing the axis description halfway between both ends of the axis. For example, take the following code:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\pgfplotsset{%
  every axis x label/.style={%
    at={(ticklabel cs:0.95)}, anchor=near ticklabel,
  },
}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xlabel={$x$},
    ylabel={$y$},
    axis x line=bottom,
    axis y line=left,
    ]
    \addplot[
    domain=-2:2,
    samples=100,
    ] {x^2};
  \end{axis}
\end{tikzpicture}
\end{document}

which produces:

enter image description here

The every axis x label sets the axis description position regardless of whether the axis is boxed or non boxed (comment out axis x line to see this). There's a every non boxed x axis key, but I believe this is for the axis line, not the description. I wasn't able to find a every non boxed axis x label key, or something similar. Of course, I could do this on a case-by-case basis (which is what I've been doing), but I'd prefer to make this a default. Is this possible?

1 Answer 1

0

I think if you put the every axis x label style inside the every non boxed x axis, it works. And duplicate it in every non boxed y axis as well, if you want that.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\pgfplotsset{%
every non boxed x axis/.style={
  every axis x label/.style={%
    at={(ticklabel cs:0.95)}, anchor=near ticklabel,
  },
},
every non boxed y axis/.style={
  every axis x label/.style={%
    at={(ticklabel cs:0.95)}, anchor=near ticklabel,
  },
},
width=7cm, height=3cm % just for example
}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xlabel={$x$},
    ylabel={$y$},
    axis x line=bottom,
    axis y line=left,
    ]
    \addplot[
    domain=-2:2,
    samples=100,
    ] {x^2};
  \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}[
    xlabel={$x$},
    ylabel={$y$},
    ]
    \addplot[
    domain=-2:2,
    samples=100,
    ] {x^2};
  \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}[
    xlabel={$x$},
    ylabel={$y$},
    axis y line=left,
    ]
    \addplot[
    domain=-2:2,
    samples=100,
    ] {x^2};
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

2
  • Exactly what I was looking for! Any chance you could explain just a little about why this works? Are nested options like this discussed somewhere in the manual? I was playing around with this a bit and noticed that setting the x axis label option in the non boxed y axis option created the effect for the x axis when the y axis was non boxed. In other words, it doesn't appear that the nested option needs to be related to the parent option. Instead, the nested option is only invoked when the parent option is invoked. Is this correct?
    – MattHusz
    Commented Jan 30, 2021 at 2:37
  • 1
    @MattHusz If I understand correctly, yes. The description of the every non boxed x axis style say "A style which will be installed as soon as axis x line will be set to something different than box." And similar for y. So whatever you put in that style gets activated when you do e.g. axis x line=bottom. Commented Jan 30, 2021 at 7:08

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .