我有代码可以生成两个 tikz 图,一个在另一个之上。它用于pgfplotset
定义轴的自定义样式,该样式将应用于两个图。
我正在尝试更改每个轴的 x 轴标签的位置之内自定义样式将标签放在轴的正下方。我知道我可以这样做在每个图中手动\coordinate
,但如果可能的话,我想避免手动执行此操作。以下这个问题和答案,我尝试将这段代码添加到样式中:
every axis x label/.style={at={(current axis.right of origin)},anchor=west},
添加此选项会使 x 标签仅显示在第一个图中,而不显示在第二个图中。我做错了什么?
代码:
\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}
输出:
答案1
您可以使用例如
xlabel style ={at={(xticklabel cs:1,0)},anchor=east,inner sep=1pt,}
如同
\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}
您可以摆脱范围并yshift
通过使用groupplots
或仅使用矩阵进行手动调整。
\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}