1

I have code that produces two tikz plots, one above the other. It uses pgfplotset to define a custom style for the axes, which is applied to both plots.

I'm trying to change the position of the x axis label for each axis within the custom style to put the label immediately below the axis. I know I could do this manually in each plot with \coordinate, but I'd like to avoid manually doing this if at all possible. Following this question and answer, I tried adding this code to the style:

every axis x label/.style={at={(current axis.right of origin)},anchor=west},

Adding this option makes the x label only show in the first plot, not the second. What am I doing wrong?

Code:

\documentclass[12pt,a4paper]{article}

\usepackage{float}
\usepackage[margin=1cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}  
\pagestyle{empty}

\pgfplotsset{customaxisstyle/.style={%
        width=18cm, height=10cm,
        title={}, xlabel={$s$}, ylabel={}, xticklabels={,,}, yticklabels={,,}, axis lines=middle,   
        xmin=0, xmax=10,
        ymin=0, ymax=5,
        domain=0:10, samples=100}}

\pgfplotsset{customlinestyle1/.style={black, thick}}
\pgfplotsset{customlinestyle2/.style={blue, line width=1mm}}

\begin{document}
\begin{figure}[H]
\begin{tikzpicture}

\begin{scope}
\begin{axis}[customaxisstyle,title={Top plot}]
\addplot[customlinestyle1] {1.8 - 0.2*x};
\addplot[customlinestyle1] {3.0 - 0.4*x};
\addplot[customlinestyle1] {2.8 - 0.6*x};
\addplot[customlinestyle1] {3.8 - 0.6*x};
\addplot[customlinestyle1] {3.8 - 0.8*x};
\addplot[customlinestyle1] {5.0 - 1.2*x};
\end{axis}
\end{scope}

\begin{scope}[yshift=-10cm]
\begin{axis}[customaxisstyle, title={Bottom plot}]
\addplot[red, very thick] { 2 * sin(deg(0.5*x + 6)) + 2.5 };
\end{axis}
\end{scope}

\end{tikzpicture}
\end{figure}
\end{document}

Output:

enter image description here

1

You can use e.g.

 xlabel style ={at={(xticklabel cs:1,0)},anchor=east,inner sep=1pt,}

as in

\documentclass[12pt,a4paper]{article}

\usepackage{float}
\usepackage[margin=1cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}  
\pagestyle{empty}

\pgfplotsset{customaxisstyle/.style={%
        width=18cm, height=10cm,
        title={}, xlabel={$s$}, ylabel={}, xticklabels={,,}, yticklabels={,,}, axis lines=middle,   
        xlabel style ={at={(xticklabel cs:1,0)},anchor=east,inner sep=1pt,},
        xmin=0, xmax=10,
        ymin=0, ymax=5,
        domain=0:10, samples=100}}

\pgfplotsset{customlinestyle1/.style={black, thick}}
\pgfplotsset{customlinestyle2/.style={blue, line width=1mm}}

\begin{document}
\begin{figure}[H]
\begin{tikzpicture}

\begin{scope}
\begin{axis}[customaxisstyle,title={Top plot}]
\addplot[customlinestyle1] {1.8 - 0.2*x};
\addplot[customlinestyle1] {3.0 - 0.4*x};
\addplot[customlinestyle1] {2.8 - 0.6*x};
\addplot[customlinestyle1] {3.8 - 0.6*x};
\addplot[customlinestyle1] {3.8 - 0.8*x};
\addplot[customlinestyle1] {5.0 - 1.2*x};
\end{axis}
\end{scope}

\begin{scope}[yshift=-10cm]
\begin{axis}[customaxisstyle, title={Bottom plot}]
\addplot[red, very thick] { 2 * sin(deg(0.5*x + 6)) + 2.5 };
\end{axis}
\end{scope}

\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

You could get rid of the scopes and manually adjusted yshift by using either groupplots or just a matrix.

\documentclass[12pt,a4paper]{article}

\usepackage{float}
\usepackage[margin=1cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}  
\pagestyle{empty}

\pgfplotsset{customaxisstyle/.style={%
        width=18cm, height=10cm,
        title={}, xlabel={$s$}, ylabel={}, xticklabels={,,}, yticklabels={,,}, axis lines=middle,   
        xlabel style ={at={(xticklabel cs:1,0)},anchor=east,inner sep=1pt,},
        xmin=0, xmax=10,
        ymin=0, ymax=5,
        domain=0:10, samples=100}}

\pgfplotsset{customlinestyle1/.style={black, thick}}
\pgfplotsset{customlinestyle2/.style={blue, line width=1mm}}

\begin{document}
\begin{figure}[H]
\begin{tikzpicture}
\matrix{
\begin{axis}[customaxisstyle,title={Top plot}]
\addplot[customlinestyle1] {1.8 - 0.2*x};
\addplot[customlinestyle1] {3.0 - 0.4*x};
\addplot[customlinestyle1] {2.8 - 0.6*x};
\addplot[customlinestyle1] {3.8 - 0.6*x};
\addplot[customlinestyle1] {3.8 - 0.8*x};
\addplot[customlinestyle1] {5.0 - 1.2*x};
\end{axis}\\
\begin{axis}[customaxisstyle, title={Bottom plot}]
\addplot[red, very thick] { 2 * sin(deg(0.5*x + 6)) + 2.5 };
\end{axis}\\
};
\end{tikzpicture}
\end{figure}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.