我想合并这个 pgf 图的两个图例,以便第二个图例“这是图例二”位于第一个图例框内。
以下是 MWE:
\documentclass[
12pt,
a4paper,
]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[dvipsnames,table,xcdraw]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\pgfplotsset{
show sum on top/.style={
/pgfplots/scatter/@post marker code/.append code={%
\node[
at={(normalized axis cs:%
\pgfkeysvalueof{/data point/x},%
\pgfkeysvalueof{/data point/y})%
},
anchor=south,
color=black,
font=\footnotesize,
]
{\pgfmathprintnumber[precision=2]{\pgfkeysvalueof{/data point/y}}};
},
},
}
\begin{axis}[
width=0.7\textwidth,
ybar stacked,
bar width=20pt,
nodes near coords={},
ymin=0, ymax=16,
legend pos=outer north east,
legend cell align={left},
legend style={font=\footnotesize,legend columns=1},
tick label style={font=\footnotesize},
xlabel style={font=\footnotesize},
ylabel style={font=\footnotesize},
ylabel={Total values},
symbolic x coords={A, B, C, D, E},
xtick=data
]
\addplot+[fill=CornflowerBlue, draw=black,ybar] plot coordinates {(A,3.34) (B,3.25) (C,1.95) (D,4.4468) (E,3.05)};
\addplot+[fill=orange, draw=black,ybar] plot coordinates {(A,0.43) (B,0.25) (C,1.06) (D,0.7987) (E,0.74)};
\addplot+[fill=YellowGreen, draw=black,ybar] plot coordinates {(A,0.97) (B,0.98) (C,2.11) (D,1.4551) (E,1.46)};
\addplot+[fill=Goldenrod, draw=black,ybar,show sum on top] plot coordinates {(A,0.6) (B,0.61) (C,1.73) (D,1.1238) (E,1.12)};
\legend{This,is,the,Legend}
\end{axis}
\begin{axis}[
width=0.7\textwidth,
axis x line=none,
axis y line=none,
ymin=0, ymax=16,
legend pos=north west,
ytick=\empty,
xtick=\empty,
symbolic x coords={A, B, C, D, E},
xtick=data
]
\addplot [Lavender,mark=diamond*, only marks, draw=black,mark size=6pt] plot coordinates {(A,0) (B,0) (C,15) (D,12) (E,12)};
\addlegendentry{This is legend two}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
最简单的方法应该是向第一个图添加自定义图例条目。您可以通过\addlegendimage
和执行此操作\addlegendentry
:
\documentclass[border=10pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
show sum on top/.style={
/pgfplots/scatter/@post marker code/.append code={%
\node[
at={(normalized axis cs:%
\pgfkeysvalueof{/data point/x},%
\pgfkeysvalueof{/data point/y})%
},
anchor=south,
color=black,
font=\footnotesize,
]
{\pgfmathprintnumber[precision=2]{\pgfkeysvalueof{/data point/y}}};
},
},
}
\begin{axis}[
width=0.7\textwidth,
ybar stacked,
bar width=20pt,
nodes near coords={},
ymin=0, ymax=16,
legend pos=outer north east,
legend cell align={left},
legend style={font=\footnotesize, legend columns=1},
tick label style={font=\footnotesize},
xlabel style={font=\footnotesize},
ylabel style={font=\footnotesize},
ylabel={Total values},
symbolic x coords={A, B, C, D, E},
xtick=data
]
\addplot+[fill=CornflowerBlue, draw=black, ybar] plot coordinates {(A,3.34) (B,3.25) (C,1.95) (D,4.4468) (E,3.05)};
\addplot+[fill=orange, draw=black, ybar] plot coordinates {(A,0.43) (B,0.25) (C,1.06) (D,0.7987) (E,0.74)};
\addplot+[fill=YellowGreen, draw=black, ybar] plot coordinates {(A,0.97) (B,0.98) (C,2.11) (D,1.4551) (E,1.46)};
\addplot+[fill=Goldenrod, draw=black, ybar, show sum on top] plot coordinates {(A,0.6) (B,0.61) (C,1.73) (D,1.1238) (E,1.12)};
\legend{This,is,the,Legend}
\addlegendimage{Lavender, mark=diamond*, only marks, draw=black, mark size=6pt, yshift=-.5em}
\addlegendentry{This is legend two}
\end{axis}
\begin{axis}[
width=0.7\textwidth,
axis x line=none,
axis y line=none,
ymin=0, ymax=16,
legend pos=north west,
ytick=\empty,
xtick=\empty,
symbolic x coords={A, B, C, D, E},
xtick=data
]
\addplot[Lavender, mark=diamond*, only marks, draw=black, mark size=6pt] plot coordinates {(A,0) (B,0) (C,15) (D,12) (E,12)};
\end{axis}
\end{tikzpicture}
\end{document}