индивидуальный маркер легенды, созданный с помощью 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}

Выход:

введите описание изображения здесь

Связанный контент