在 pgfplot 圖例中使用繪圖而非文字

在 pgfplot 圖例中使用繪圖而非文字

我很好奇是否可以使用自訂圖像(例如繪圖)而不是使用 pgfplots 生成的圖表圖例內的文字。具體來說,考慮到本次 MWC:

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}
    \tikzset{every mark/.append style={scale=0.75}}
    \begin{tikzpicture}

    \begin{axis}[xlabel=$x$, ymin=0, xmin=-1.1, xmax=1.1, samples=300, ylabel=$f(x)$,
                 smooth,
                 legend style={ legend cell align=left, at={(1.03, 1)}, anchor=north west}
                 ]
        \addplot coordinates {
                              (-1, 0)
                              (0, 0.5)
                              (1, 1)
                              };
        \addlegendentry{Uniform(0,1)};
        \addplot coordinates {
                              (-1, 1)
                              (0, 0.2)
                              (1, 0)
                              };
        \addlegendentry{Exponential(1)};
    \end{axis}
    \end{tikzpicture}
\end{document}

產生下面的圖表

http://i.imgur.com/T2zVNe5.png

我想在圖例文字中插入均勻分佈的實際密度函數,例如我想看到這個(縮小)

均勻分佈的 PDF

而不是“Uniform(0, 1)”文本。當然,指數(1)部分類似。

統一 PDF 圖也是使用 pgfplots(下面的 MWC)產生的,因此問題也許是是否可以將 pgfplot 嵌入到圖例條目中。

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}
    \tikzset{every mark/.append style={scale=0.75}}
    \begin{tikzpicture}

    \begin{axis}[ymin=0, xmin=0, xmax=1, ymax=2,xticklabels=none, yticklabels=none]
        \addplot[color=black, fill=black] (x,1) \closedcycle;
    \end{axis}
    \end{tikzpicture}
\end{document}

稍後編輯

托比昂提出的解決方案有效;它做我想要的:

圖例中包含兩個影像的繪圖

我將開放這個問題,看看是否有另一種可能性可以動態產生所有內容,而不是嵌入先前編譯的檔案。

答案1

(這是我最初答案的完全重寫版本)

為了修改圖例條目的描述,可以使用\addlegendentrylegend entries\legend。因此,@Torbjorns 的答案是正確的選擇。

但是,您可以隨意在圖例文字中插入小圖片;文字沒有限制。為此,您可以簡單地寫成\tikz ... ;“Uniform”而不是“Uniform”。

這是一種基於 @Torbjorns 答案和我嘗試組裝或多或少合適的圖像的方法:

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}
    \tikzset{every mark/.append style={scale=0.75}}
    \begin{tikzpicture}

    \begin{axis}[xlabel=$x$, ymin=0, xmin=-1.1, xmax=1.1, samples=300, ylabel=$f(x)$,
                 smooth,
                 legend style={ legend cell align=left, at={(1.03, 1)}, anchor=north west}
                 ]
        \addplot coordinates {
                              (-1, 0)
                              (0, 0.5)
                              (1, 1)
                              };
        \addlegendentry{
                \begin{tikzpicture}
                    \fill[draw] (0cm,0cm) rectangle (0.6cm,-0.15cm);
                    \draw (0cm,0cm) rectangle (0.6cm,+0.15cm);
                \end{tikzpicture}
        }
        \addplot  coordinates {
                              (-1, 1)
                              (0, 0.2)
                              (1, 0)
                              };
        \addlegendentry{
                \tikz\draw[mark=none,samples=11,domain=-2.5:2.5,xshift=1em,yshift=-0.05cm,xscale=0.015,yscale=0.03]  plot (\x,{exp(-(\x)^2)});
        }
    \end{axis}
    \end{tikzpicture}
\end{document}

在此輸入影像描述

答案2

不是直接答案,但至少是一個解決方法:您可以\includegraphics在圖例條目中使用,因此您可以先編譯以產生均勻分佈的程式碼,然後使用

\addlegendentry{\includegraphics[width=1cm]{FilenameOfPDFwithUniformDist}};

在圖例中新增圖像。

相關內容