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}

그러면 다음과 같은 출력이 생성됩니다.

영상

나는 전설을 맨 아래에 자유롭게 배치했습니다. 미안합니다. 개인적으로 \addlegendimageTikZ 그리기 명령을 직접 사용하지 않아 보간점에 대한 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}

산출:

여기에 이미지 설명을 입력하세요

관련 정보