我正在pgfplots
使用 TikZ对齐许多\matrix
。轴会自动正确对齐。我如何才能自动对齐ylabels
(因为我经常更改绘制的数据)?
\usepackage[latin1]{inputenc}
\usepackage[USenglish]{babel}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=newest}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{figure} [H]
\centering
\begin{tikzpicture}
\matrix{
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1}]
\addplot {x^2};
\end{axis}
\\
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2}]
\addplot {-(x^2)};
\end{axis}
\\
};
\end{tikzpicture}
\end{figure}
\end{document}
不期望的输出:
期望输出:
我再次仔细观察了:
- 手册作者
pgfplots
:Christian Feuersaenger - Till Tantau手册
TikZ
及作者PGF
编辑
谢谢!尤其是精细的控制真的很好。
不幸的是,现在 -- 有了这个ylabel absolute
-- 用\\
或换行\newline
已经不可能了 :-(
有什么诀窍吗?
无效的代码:
\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{
compat=newest,
/pgfplots/myylabel absolute/.style={%
/pgfplots/every axis y label/.style={at={(0,0.5)},xshift=#1,rotate=90},
/pgfplots/every y tick scale label/.style={
at={(0,1)},above right,inner sep=0pt,yshift=0.3em
}
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\matrix{
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1 first line\\second line},myylabel absolute=-60]
\addplot {x^2};
\end{axis}
\\
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2 first line\\second line},,myylabel absolute=-60]
\addplot {-(x^2)};
\end{axis}
\\
};
\end{tikzpicture}
\end{figure}
\end{document}
答案1
该ylabel absolute
选项(手册第 173 和 174 页pgfplots
)产生所需的结果:
\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=newest}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\matrix{
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1},ylabel absolute]
\addplot {x^2};
\end{axis}
\\
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2},ylabel absolute]
\addplot {-(x^2)};
\end{axis}
\\
};
\end{tikzpicture}
\end{figure}
\end{document}
如果您想要更精细的控制,您可以以类似的方式定义一种样式ylabel absolute
,但允许您通过参数指定所需的移位(ylabel absolute
使用-35pt
):
\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{
compat=newest,
/pgfplots/myylabel absolute/.style={%
/pgfplots/every axis y label/.style={at={(0,0.5)},xshift=#1,rotate=90},
/pgfplots/every y tick scale label/.style={
at={(0,1)},above right,inner sep=0pt,yshift=0.3em
}
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\matrix{
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1},myylabel absolute=-30pt]
\addplot {x^2};
\end{axis}
\\
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2},,myylabel absolute=-30pt]
\addplot {-(x^2)};
\end{axis}
\\
};
\end{tikzpicture}
\begin{tikzpicture}
\matrix{
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1},myylabel absolute=-40pt]
\addplot {x^2};
\end{axis}
\\
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2},,myylabel absolute=-40pt]
\addplot {-(x^2)};
\end{axis}
\\
};
\end{tikzpicture}
\end{figure}
\end{document}
对原始问题进行编辑后:问题在于您尝试使用包含多行文本的标签;为了做到这一点,您可以例如使用align=<value>
标签选项:
\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{
compat=newest,
/pgfplots/myylabel absolute/.style={%
/pgfplots/every axis y label/.style={at={(0,0.5)},xshift=#1,rotate=90,align=center},
/pgfplots/every y tick scale label/.style={
at={(0,1)},above right,inner sep=0pt,yshift=0.3em
}
}
}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\matrix{
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1 first line\\second line},myylabel absolute=-50]
\addplot {x^2};
\end{axis}
\\
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2 first line\\second line},myylabel absolute=-50]
\addplot {-(x^2)};
\end{axis}
\\
};
\end{tikzpicture}
\end{figure}
\end{document}