將自訂條目新增至 pgfplot 中的圖例中

將自訂條目新增至 pgfplot 中的圖例中

我想在圖例中添加一些手動條目,以解釋圖左側的字元“A”和“B”。我不知道在哪裡放置它們的定義。

在此輸入影像描述

因此,圖例中的「Plot E」下方應該是類似「A = ...」的內容。有什麼辦法可以做到這一點嗎?或是有其他選擇嗎?

答案1

\addlegendimage和的組合\addlegendentry允許添加自訂條目。第一個的目的是添加圖形選項,第二個的目的是添加描述文字。

在您的情況下,小圖例圖像可能只是文字“A”或“B”,而描述文字將......好吧,描述這些群組。

人們可以定義一種不使用填充區域或小圖例圖像的樣式,而只是使用帶有文字的節點:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\pgfplotsset{
    legend image with text/.style={
        legend image code/.code={%
            \node[anchor=center] at (0.3cm,0cm) {#1};
        }
    },
}

\begin{document}

\begin{tikzpicture}
\begin{semilogyaxis}[
    domain=0:4,
]
    \addplot {x};   \addlegendentry{$x$}
    \addplot {x^2}; \addlegendentry{$x^2$}
    \addplot {x^3}; \addlegendentry{$x^3$}
    \addlegendimage{legend image with text=A}
    \addlegendentry{$= 42$}
    \addlegendimage{legend image with text=B}
    \addlegendentry{$\approx 43$}
    \addplot {x^(-1)}; \addlegendentry{$x^{-1}$}
    \addplot {x^(-2)}; \addlegendentry{$x^{-2}$}
    \addplot {x^(-3)}; \addlegendentry{$x^{-3}$}
\end{semilogyaxis}
\end{tikzpicture}

\end{document}

在此輸入影像描述

相關內容