只有一些分散的標記和正確的圖例的圖

只有一些分散的標記和正確的圖例的圖

我想用線條和幾個標記來繪製幾張圖。標記的間距不規則,因此我使用了 pgfplots 手冊第 4.8.5 節(圖例外觀)第 159 頁條目中提到的方法 every legend image post。線條和標記由不同的\addplot命令創建。此方法的問題是要有一個顯示線條和標記的圖例。

以下是 pgfplots 手冊中提供的代碼的改編:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend image post style={mark=*}]
\addplot+[only marks,forget plot]coordinates {(0.5,-0.5) (1,-1) (1.5,-1.5)};
\addplot+[mark=none,smooth,domain=0:2]{-x};
\addlegendentry{Parabola}

\addplot+[only marks,forget plot]coordinates {(0.3,0.3) (0.45,0.45) (1.7,1.7)};
\addplot+[mark=none,smooth,domain=0:2]{x};
\addlegendentry{Parabola}
\end{axis}
\end{tikzpicture}
\end{document}

當只有一條曲線時,此程式碼有效,因為可以在legend image post style關鍵影格中指定繪圖標記。但是,您知道如何將其適應具有不同情節標記的多個情節嗎?

答案1

legend image post style透過為每個命令提供\addplot而不是axis,它僅適用於每個單獨的圖:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+[only marks,forget plot]coordinates {(0.5,-0.5) (1,-1) (1.5,-1.5)};
\addplot+[mark=none,smooth,domain=0:2,legend image post style={mark=*}]{-x};
\addlegendentry{Parabola}

\addplot+[only marks,forget plot]coordinates {(0.3,0.3) (0.45,0.45) (1.7,1.7)};
\addplot+[mark=none,smooth,domain=0:2,legend image post style={mark=square*}]{x};
\addlegendentry{Parabola}
\end{axis}
\end{tikzpicture}
\end{document}

相關內容