
我想將樹的元素新增到圖例中(有關說明,請參閱圖例)。我發現這和這答案,但他們沒有解釋如何為圖例添加其他形狀。我在 pgfplots-docs 中沒有找到任何有用的東西。我不限於圖例的 pgfplots,如果您有其他解決方案,我將很樂意使用它。
\documentclass{standalone}
\usepackage[dvipsnames,fixpdftex]{xcolor}
\usepackage{forest}
\usetikzlibrary{calc,shadings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
% Code from Christian Feuersänger
% https://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture#54834
% argument #1: any options
\newenvironment{customlegend}[1][]{%
\begingroup
% inits/clears the lists (which might be populated from previous
% axes):
\csname pgfplots@init@cleared@structures\endcsname
\pgfplotsset{#1}%
}{%
% draws the legend:
\csname pgfplots@createlegend\endcsname
\endgroup
}%
% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}
%%--------------------------------
% definition to insert numbers
\pgfkeys{/pgfplots/number in legend/.style={%
/pgfplots/legend image code/.code={%
\node at (0.295,-0.0225){#1};
},%
},
}
\begin{document}
\begin{forest}
for tree={circle,draw,minimum size = 2.25em, l sep=15pt, s sep=3.0em}
[0, fill=Apricot
[1, fill=SpringGreen, shape=diamond, for tree={s sep = 3.0em}
[$\dots$, draw=none]
]
[2, fill=SpringGreen, shape=star, for tree={s sep = 3.0em}
[$\dots$, draw=none]
[8]
]
[3, fill=SpringGreen, shape=diamond
[$\dots$, draw=none]
]
[4, fill=Apricot
[$\dots$, draw=none]
]
[5, fill=SpringGreen, shape=regular polygon
[$\dots$, draw=none]
]
]
\begin{customlegend}[
legend entries={ % <= in the following there are the entries
Green circle/node here,
Red circle/node here,
White Star here,
White Diamond here,
White Polygon here
},
legend style={at={(4.5,3.5)},font=\footnotesize}] % <= to define position and font legend
% the following are the "images" and numbers in the legend
\addlegendimage{mark=ball,ball color=SpringGreen, draw=white}
\addlegendimage{mark=ball,ball color=Apricot, draw=white}
\addlegendimage{stealth-stealth,red,opacity=0.4}
\addlegendimage{number in legend=1,blue}
\addlegendimage{number in legend=0,red}
\end{customlegend}
\end{forest}
\end{document}
答案1
您可以在圖例中新增與目前樣式類似的形狀樣式number in legend
,例如稱為shape in legend
。可以使用 定義形狀的樣式以接受兩個參數:形狀和顏色.style 2 args
。這被稱為shape in legend={arg1}{arg2}
(參見如何使用 pgfplots 繪製來自多個過濾器的數據)。該樣式本身繪製一個使用形狀和填滿顏色作為參數的普通節點。
微量元素:
\documentclass{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{forest}
\usetikzlibrary{calc,shadings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
% Code from Christian Feuersänger
% https://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture#54834
% argument #1: any options
\newenvironment{customlegend}[1][]{%
\begingroup
% inits/clears the lists (which might be populated from previous
% axes):
\csname pgfplots@init@cleared@structures\endcsname
\pgfplotsset{#1}%
}{%
% draws the legend:
\csname pgfplots@createlegend\endcsname
\endgroup
}%
% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}
%%--------------------------------
% definition to insert numbers and shapes
\pgfkeys{/pgfplots/number in legend/.style={%
/pgfplots/legend image code/.code={%
\node at (0.295,-0.0225){#1};
},%
},
/pgfplots/shape in legend/.style 2 args={%
/pgfplots/legend image code/.code={%
\node[draw,#1,fill=#2,minimum width=3mm] at (0.295,-0.0225){};
},%
},
}
\begin{document}
\begin{forest}
for tree={circle,draw,minimum size = 2.25em, l sep=15pt, s sep=3.0em}
[0, fill=Apricot
[1, fill=SpringGreen, shape=diamond, for tree={s sep = 3.0em}
[$\dots$, draw=none]
]
[2, fill=SpringGreen, shape=star, for tree={s sep = 3.0em}
[$\dots$, draw=none]
[8]
]
[3, fill=SpringGreen, shape=diamond
[$\dots$, draw=none]
]
[4, fill=Apricot
[$\dots$, draw=none]
]
[5, fill=SpringGreen, shape=regular polygon
[$\dots$, draw=none]
]
]
\begin{customlegend}[
legend entries={ % <= in the following there are the entries
Green circle/node here,
Red circle/node here,
White Star here,
White Diamond here,
White Polygon here
},
legend style={at={(4.5,3.5)},font=\footnotesize}] % <= to define position and font legend
% the following are the "images" and numbers in the legend
\addlegendimage{shape in legend={circle}{SpringGreen}}
\addlegendimage{shape in legend={circle}{red}}
\addlegendimage{shape in legend={star}{white}}
\addlegendimage{shape in legend={diamond}{white}}
\addlegendimage{shape in legend={regular polygon}{white}}
\end{customlegend}
\end{forest}
\end{document}
結果: