我有一张图,其中展示了实验获得的数据和不同的数据模型。为了保持图例清晰,我想在一个图例条目下总结同一模型的略有不同的版本。
我偶然发现了这类似的问题,但建议的解决方案导致所有图例图像都更改为中给出的图像legend image code/.code
。 我也尝试添加空的图例条目,但这会导致标签与第一个图例图像对齐。
以下是具有当前图例的 MWE:
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend cell align={left},
legend style={at={(0.98,0.02)}, anchor=south east, draw=black},
xmin=0, xmax=1,
ymin=0, ymax=1,
]
\addlegendimage{only marks, black, mark=square}
\addlegendentry{Experiment};
\addlegendimage{blue}
\addlegendentry{Model 1-1};
\addlegendimage{blue,dashed}
\addlegendentry{Model 1-2};
\addlegendimage{blue, dotted}
\addlegendentry{Model 1-3};
\addlegendimage{only marks, blue, mark=x}
\addlegendentry{Model max};
\addlegendimage{red}
\addlegendentry{Model 2}
\addplot [black, mark=square, mark options={solid}, only marks]
table {%
0 0.1
0.25 0.4
0.5 0.6
0.75 0.7
1 0.9
};
\addplot [blue]
table {%
0 0
1 1.5
};
\addplot [blue, dashed]
table {%
0 0
1 0.9
};
\addplot [blue, dotted]
table {%
0 0
1 0.7
};
\addplot [blue, mark=x, mark options=solid, only marks]
table {%
1 0.95
};
\addplot [red]
table {%
0 0
0.1 0.3
0.2 0.5
0.3 0.6
0.4 0.65
0.5 0.7
1 1
};
\end{axis}
\end{tikzpicture}
\end{document}
我想要的图例如下:
答案1
可以pgfkeys
像这样定义新的图例条目样式:
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfkeys{% inspired by texmf-dist/tex/generic/pgfplots/pgfplots.code.tex
/pgfplots/three lines legend/.style={%
/pgfplots/legend image code/.code={%
\draw[##1] (0cm, 0.1cm) -- (0.6cm, 0.1cm);
\draw[##1,dashed] (0cm, 0.0cm) -- (0.6cm, 0.0cm);
\draw[##1,dotted] (0cm,-0.1cm) -- (0.6cm,-0.1cm);
}%
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend cell align={left},
legend style={at={(0.98,0.02)}, anchor=south east, draw=black},
xmin=0, xmax=1,
ymin=0, ymax=1,
]
\addlegendimage{only marks, black, mark=square}
\addlegendentry{Experiment};
\addlegendimage{three lines legend,blue}
\addlegendentry{Model 1-i};
\addlegendimage{only marks, blue, mark=x}
\addlegendentry{Model max};
\addlegendimage{red}
\addlegendentry{Model 2}
\addplot [black, mark=square, mark options={solid}, only marks]
table {%
0 0.1
0.25 0.4
0.5 0.6
0.75 0.7
1 0.9
};
\addplot [blue]
table {%
0 0
1 1.5
};
\addplot [blue, dashed]
table {%
0 0
1 0.9
};
\addplot [blue, dotted]
table {%
0 0
1 0.7
};
\addplot [blue, mark=x, mark options=solid, only marks]
table {%
1 0.95
};
\addplot [red]
table {%
0 0
0.1 0.3
0.2 0.5
0.3 0.6
0.4 0.65
0.5 0.7
1 1
};
\end{axis}
\end{tikzpicture}
\end{document}