我想在 pgfplots 中为图例的每一行添加一个标题。我通过以下示例实现了此目的:
\documentclass{article}
\usepackage[pdftex, active, floats, tightpage]{preview}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
axis on top,
axis equal image,
xmin=-1, xmax=1.05, ymin=-0.05, ymax=1.5
,axis x line=bottom, axis y line=left
, legend columns=3, legend style={cells={anchor=west}, at={(0.5,1)}, anchor=north, draw=none, name=legend_name
,width=\textwidth
]
\addplot [color=blue, thick, mark=none, domain=-1:1, samples=100] {x^2};
\addlegendentry{$x^2$}
\addplot [color=blue, thick, loosely dashed, mark=none, domain=-1:1, samples=100] {x^3};
\addlegendentry{$x^3$}
\addplot [color=blue, thick, dotted, mark=none, domain=-1:1, samples=100] {x^4};
\addlegendentry{$x^4$}
\addplot [color=red, thick, mark=none, domain=-1:1, samples=100] {2*x^2};
\addlegendentry{$2 x^2$}
\addplot [color=red, thick, loosely dashed, mark=none, domain=-1:1, samples=100] {2*x^3};
\addlegendentry{$2 x^3$}
\addplot [color=red, thick, dotted, mark=none, domain=-1:1, samples=100] {2*x^4};
\addlegendentry{$2 x^4$}
\end{axis}
\node (legend_row_title) at (legend_name.west) [anchor=east, align=center, xshift=2pt] {1\textsuperscript{st} row :\\2\textsuperscript{nd} row :};
\draw (legend_row_title.north west) rectangle (legend_name.south east);
\end{tikzpicture}
\end{figure}
\end{document}
但问题是,用于对齐图例的锚点是图例本身的中心,因此矩形和行标题对齐不正确。有人知道如何正确对齐吗?或者至少修复对齐问题?谢谢!
答案1
您可以使用
\addlegendimage{empty legend}
\addlegendentry{1\textsuperscript{st} row :}
在适当的位置添加虚拟图例:
\documentclass{article}
\usepackage[pdftex, active, floats, tightpage]{preview}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
axis on top,
axis equal image,
xmin=-1, xmax=1.05, ymin=-0.05, ymax=1.5
,axis x line=bottom, axis y line=left
, legend columns=4, legend style={cells={anchor=west}, at={(0.5,1)}, anchor=north, draw=none, name=legend_name,draw}
,width=\textwidth
]
\addlegendimage{empty legend}
\addlegendentry{1\textsuperscript{st} row :}
\addplot [color=blue, thick, mark=none, domain=-1:1, samples=100] {x²};
\addlegendentry{$x²$}
\addplot [color=blue, thick, loosely dashed, mark=none, domain=-1:1, samples=100] {x³};
\addlegendentry{$x³$}
\addplot [color=blue, thick, dotted, mark=none, domain=-1:1, samples=100] {x⁴};
\addlegendentry{$x⁴$}
\addlegendimage{empty legend}
\addlegendentry{2\textsuperscript{nd} row :}
\addplot [color=red, thick, mark=none, domain=-1:1, samples=100] {2*x²};
\addlegendentry{$2 x²$}
\addplot [color=red, thick, loosely dashed, mark=none, domain=-1:1, samples=100] {2*x³};
\addlegendentry{$2 x³$}
\addplot [color=red, thick, dotted, mark=none, domain=-1:1, samples=100] {2*x⁴};
\addlegendentry{$2 x⁴$}
\end{axis}
%\node (legend_row_title) at (legend_name.west) [text width=4em,anchor=east, align=right, xshift=2pt] {\strut 1\textsuperscript{st} row :\\2\textsuperscript{nd} row :};
% \draw (legend_row_title.north west) rectangle (legend_name.south east);
\end{tikzpicture}
\end{figure}
\end{document}