如何在 pgfplots 的 groupplot 中添加图例?

如何在 pgfplots 的 groupplot 中添加图例?

我正在使用 制作分组图groupplot。我喜欢这个图形,但我无法正确放置图例。以下是代码:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{makeidx}
\usepackage{graphicx}
\usepackage{times}
\usepackage[brazil]{babel}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{filecontents}

\begin{document}

\begin{filecontents*}{dose_sulfato.csv}
Dosagem SA, Turbidez,   Cor,    Potencial,  Remocao T,      Remocao C,
7.8,        2.75,       35,     -5.43,      68.85617214,    79.76878613
8.4,        1.78,       39,     -3.69,      79.8414496,     77.4566474
9,          1.57,       30,     -5.04,      82.21970555,    82.65895954
9.6,        1.3,        27,     -0.746,     85.27746319,    84.39306358
10.2,       1.67,       28,     -2.71,      81.08720272,    83.8150289
\end{filecontents*}
%
\begin{filecontents*}{dose_quitosana.csv}
Dosagem Q,  Turbidez,   Cor,    Potencial,  Remocao T,      Remocao C
0.5,        1.32,       22,     -6.61,      85.05096263,    87.28323699
0.7,        0.7,        10,     -2.48,      92.07248018,    94.21965318
\end{filecontents*}

\begin{tikzpicture}
\begin{groupplot}[
group style={group size=2 by 2},
width=0.4\textwidth,
xtick=data,
]
\nextgroupplot[title=Turbidez]
\addplot table[x=Dosagem SA, y=Remocao T, col sep=comma,] {dose_sulfato.csv};
\nextgroupplot[title=Cor Aparente]
\addplot table[x=Dosagem SA, y=Remocao C, col sep=comma,] {dose_sulfato.csv};
\nextgroupplot
\addplot[red, mark=*] table[x=Dosagem Q, y=Remocao T, col sep=comma,] {dose_quitosana.csv};
\nextgroupplot
\addplot[red, mark=*] table[x=Dosagem Q, y=Remocao C, col sep=comma,] {dose_quitosana.csv};    
\end{groupplot}
\node[anchor=north] (title-x) at ($(group c1r2.south east)!0.5!(group c2r2.south west)-(0,0.5cm)$) {Dosagem de Coagulante $(mg/L)$};
\node[anchor=south, rotate=90] (title-y) at ($(group c1r1.south west)!0.5!(group c1r2.north west)-(0.85,0cm)$) {Remoção (\%)};
\end{tikzpicture}

\end{document}

我越来越:

组p1

但我想得到类似的东西

在此处输入图片描述

我不知道如何用这种方式表达图例。有人能帮我吗?

编辑 1:图例可以放置在图形中的任何位置

答案1

您可以legend to name按照 John Kormylo 在评论中建议的方式使用,但还需要一个额外的步骤。我认为您无法从多个轴收集图例条目,因此您可以使用\addlegendimage{<plot options>}不绘制任何内容但会生成图例条目的。例如,将第一个修改groupplot

\nextgroupplot[title=Turbidez,legend to name={CommonLegend},legend style={legend columns=2}]
\addplot table[x=Dosagem SA, y=Remocao T, col sep=comma,] {dose_sulfato.csv};
\addlegendimage{red, mark=*}
\addlegendentry{Sulfato}
\addlegendentry{Quitosana}

注意新选项。要打印图例,请使用\ref{CommonLegend},您可以将其添加到任意位置的节点中。我将其放在两行图之间,使用

\path (group c1r2.north east) -- node[above]{\ref{CommonLegend}} (group c2r2.north west);

vertical sep=1.5cm为了给它腾出空间,我按照 John Kormylo 提到的做了,并增加了轴之间的垂直空间group style

最后,一个不相关的说明:单位通常不应该用斜体排版,所以我删除了周围的美元符号mg/L

在此处输入图片描述

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{times}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{filecontents}

\begin{document}

\begin{filecontents*}{dose_sulfato.csv}
Dosagem SA, Turbidez,   Cor,    Potencial,  Remocao T,      Remocao C,
7.8,        2.75,       35,     -5.43,      68.85617214,    79.76878613
8.4,        1.78,       39,     -3.69,      79.8414496,     77.4566474
9,          1.57,       30,     -5.04,      82.21970555,    82.65895954
9.6,        1.3,        27,     -0.746,     85.27746319,    84.39306358
10.2,       1.67,       28,     -2.71,      81.08720272,    83.8150289
\end{filecontents*}
%
\begin{filecontents*}{dose_quitosana.csv}
Dosagem Q,  Turbidez,   Cor,    Potencial,  Remocao T,      Remocao C
0.5,        1.32,       22,     -6.61,      85.05096263,    87.28323699
0.7,        0.7,        10,     -2.48,      92.07248018,    94.21965318
\end{filecontents*}

\begin{tikzpicture}
\begin{groupplot}[
group style={group size=2 by 2,vertical sep=1.5cm},
width=0.4\textwidth,
xtick=data,
]
\nextgroupplot[title=Turbidez,legend to name={CommonLegend},legend style={legend columns=2}]
\addplot table[x=Dosagem SA, y=Remocao T, col sep=comma,] {dose_sulfato.csv};
\addlegendimage{red, mark=*}
\addlegendentry{Sulfato}
\addlegendentry{Quitosana}

\nextgroupplot[title=Cor Aparente]
\addplot table[x=Dosagem SA, y=Remocao C, col sep=comma,] {dose_sulfato.csv};
\nextgroupplot
\addplot[red, mark=*] table[x=Dosagem Q, y=Remocao T, col sep=comma,] {dose_quitosana.csv};
\nextgroupplot
\addplot[red, mark=*] table[x=Dosagem Q, y=Remocao C, col sep=comma,] {dose_quitosana.csv};    
\end{groupplot}
\node[anchor=north] (title-x) at ($(group c1r2.south east)!0.5!(group c2r2.south west)-(0,0.5cm)$) {Dosagem de Coagulante (mg/L)};
\node[anchor=south, rotate=90] (title-y) at ($(group c1r1.south west)!0.5!(group c1r2.north west)-(0.85,0cm)$) {Remoção (\%)};

\path (group c1r2.north east) -- node[above]{\ref{CommonLegend}} (group c2r2.north west);
\end{tikzpicture}

\end{document}

相关内容