例子:
\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}
输出: