使用 tikzset 建立的自訂圖例標記

使用 tikzset 建立的自訂圖例標記

例子:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{xcolor}


% Define TikZ styles for interpolation points and values
\newcommand{\interpolationPointStyle}{%
    \tikzset{interpolation point/.style={circle, inner sep=1.5pt, fill=black}}
}

\begin{document}

\begin{tikzpicture}

\interpolationPointStyle

\begin{axis}[
    xlabel={$x$},
    ylabel={$y$},
    ymin=0,
    xtick=\empty,
    axis background/.style={fill=white},
clip=false,
]

% Define array of tick positions
\def\myxticks{3, 4, 5}
% Loop over tick positions and draw interpolation points
\foreach \tick in \myxticks {
    \edef\tmp{\noexpand\node[interpolation point] at (axis cs:\tick,0) {};}
    \tmp
}
% Curve 1
\addplot[color=red, mark=none, smooth] coordinates {
    (3, 0.5)
    (4, 0.6)
    (5, 0.4)
};
\addlegendentry{Curve 1}

% Interpolation points
\addlegendimage{interpolation point}
\addlegendentry{Interpolation Points}

\end{axis}
\end{tikzpicture}

\end{document}

產生輸出

在此輸入影像描述

問題:

我想要在圖例中的 x 軸上繪製的插值點符號。我認為這可以透過將其放置在圖例圖像中來實現,但會出現一條直線。

必須糾正什麼?

答案1

這是我對您的程式碼的修改:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{xcolor}

% Define TikZ styles for interpolation points and values
\newcommand{\interpolationPointStyle}{%
    \tikzset{interpolation point/.style={circle, inner sep=1.5pt, fill=black}}
}

\begin{document}

\begin{tikzpicture}

\interpolationPointStyle

\begin{axis}[
    xlabel={$x$},
    ylabel={$y$},
    ymin=0,
    xtick=\empty,
    axis background/.style={fill=white},
    clip=false,
    legend style={at={(0.5,-0.2)}, anchor=north, legend columns=-1}
]

% Define array of tick positions
\def\myxticks{3, 4, 5}
% Loop over tick positions and draw interpolation points
\foreach \tick in \myxticks {
    \edef\tmp{\noexpand\node[interpolation point] at (axis cs:\tick,0) {};}
    \tmp
}
% Curve 1
\addplot[color=red, mark=none, smooth] coordinates {
    (3, 0.5)
    (4, 0.6)
    (5, 0.4)
};
\addlegendentry{Curve 1}

% Manually create legend entry for interpolation points
\addlegendimage{only marks, mark=*, mark options={fill=black, scale=0.8}}
\addlegendentry{Interpolation Points}

\end{axis}
\end{tikzpicture}

\end{document}

這應該會產生如下輸出:

影像

我冒昧地將圖例放在底部。對於那個很抱歉。我個人認為您遇到問題是因為\addlegendimage不直接使用 TikZ 繪圖命令,因此它無法識別插值點的 TikZ 樣式。

但這只是我的理論,我不是 100% 確定。

答案2

我找到了解決我的問題的方法:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{xcolor}


% Define TikZ styles for interpolation points
\newcommand{\interpolationPointStyle}{%
    \tikzset{interpolation point/.style={circle, inner sep=1.5pt, fill=black}}
}

% Define custom legend styles for interpolation points 
\pgfplotsset{
    interpolation point legend/.style={
        legend image code/.code={
            \node[interpolation point] at (0.3cm,0cm) {};
        }
    },
}

\begin{document}

\begin{tikzpicture}

\interpolationPointStyle

\begin{axis}[
    xlabel={$x$},
    ylabel={$y$},
    ymin=0,
    xtick=\empty,
    axis background/.style={fill=white},
clip=false,
]

% Define array of tick positions
\def\myxticks{3, 4, 5}
% Loop over tick positions and draw interpolation points
\foreach \tick in \myxticks {
    \edef\tmp{\noexpand\node[interpolation point] at (axis cs:\tick,0) {};}
    \tmp
}
% Curve 1
\addplot[color=red, mark=none, smooth] coordinates {
    (3, 0.5)
    (4, 0.6)
    (5, 0.4)
};
\addlegendentry{Curve 1}

\addlegendimage{interpolation point legend}
\addlegendentry{Interpolation Points}

\end{axis}
\end{tikzpicture}

\end{document}

輸出:

在此輸入影像描述

相關內容