使用groupplots
中的库pgfplots
,如何为图组设置标题?以下代码(基本上来自手册)是一个示例布局。我不希望每个图都有一个标题,而是希望将标题放在组的顶部中央。
\documentclass[crop,tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
{group size=2 by 2}},
height=3cm,width=3cm,
title={Title}]
\nextgroupplot
\addplot coordinates {(0,0) (1,1) (2,2)};
\nextgroupplot
\addplot coordinates {(0,2) (1,1) (2,0)};
\nextgroupplot
\addplot coordinates {(0,2) (1,1) (2,1)};
\nextgroupplot
\addplot coordinates {(0,2) (1,1) (1,0)};
\end{groupplot}
\end{tikzpicture}
\end{document}
答案1
您可以使用 TikZ 节点作为标题来快速解决问题。
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 2},height=3cm,width=3cm]
\nextgroupplot[title=One]
\addplot coordinates {(0,0) (1,1) (2,2)};
\nextgroupplot[title=Two]
\addplot coordinates {(0,2) (1,1) (2,0)};
\nextgroupplot[title=Three]
\addplot coordinates {(0,2) (1,1) (2,1)};
\nextgroupplot[title=Four]
\addplot coordinates {(0,2) (1,1) (1,0)};
\end{groupplot}
\node (title) at ($(group c1r1.center)!0.5!(group c2r1.center)+(0,2cm)$) {THE Title};
\end{tikzpicture}
\end{document}
答案2
您还可以使用\matrix
命令将图表分组到单个图表中tickzpicture
,并将标题添加到创建的 tickzpicture:
% Preamble: \pgfplotsset{width=7cm,compat=1.9}
\begin{tikzpicture}
\pgfplotsset{small}
\matrix {
\begin{axis}
\addplot {x};
\end{axis}
&
% differently large labels are aligned automatically:
\begin{axis}[ylabel={$f(x)=x^2$},ylabel style={font=\Huge}]
\addplot {x^2};
\end{axis}
\\
%
\begin{axis}[xlabel=$x$,xlabel style={font=\Huge}]
\addplot {x^3};
\end{axis}
&
\begin{axis}
\addplot {x^4};
\end{axis}
\\
};
\end{tikzpicture}
(这里不添加标题,但你只需要将其添加到矩阵命令的顶部)有关PGFPLOTS
包的更多信息,请参见PGFPlot 手册。