比如说,用函数项绘制整数序列的最佳方法是什么2*m+2
?
我试过
\addplot[variable=\m, only marks, red] {m==int(m) ? 2*m+2 : nan};
但这并没有画出任何东西。
\documentclass[border=5pt, tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[font=\footnotesize,
declare function={
a(\m)=(\m==int(\m) ? 2*\m+2 : nan); },
]
\begin{axis}[
xlabel={$m$},
ylabel={$a(m)$},
title={$a(m)=2m+2$, only normal curve is showing},
axis lines=middle, %
axis x line=bottom, % show 0 at origin
xmin=0, xmax=9.5,
ymin=0, ymax=13,
xlabel style={anchor=north west},
ylabel style={anchor=south east},
%grid=major,
%%enlarge x limits={abs=1.1, upper},
enlarge y limits={rel=0.1, upper},
]
% Normal curve
\addplot[variable=\m, thin, blue] {2*m+2};
% Does not work:
\addplot[variable=\m, only marks, red] {m==int(m) ? 2*m+2 : nan};
% Does not work:
\addplot[variable=\m, densely dashed, green] {a(\m)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
samples
PGFPlots 计算由或控制的有限数量的点中的函数值samples at
。这些点不一定包含整数,但你可以samples at
像这样强制它:
\documentclass[border=5pt, tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[font=\footnotesize,
declare function={
a(\m)=2*\m+2; },
]
\begin{axis}[
xlabel={$m$},
ylabel={$a(m)$},
title={$a(m)=2m+2$, not only normal curve is showing},
axis lines=middle, %
axis x line=bottom, % show 0 at origin
xmin=0, xmax=9.5,
ymin=0, ymax=13,
xlabel style={anchor=north west},
ylabel style={anchor=south east},
%grid=major,
%%enlarge x limits={abs=1.1, upper},
enlarge y limits={rel=0.1, upper},
]
% Normal curve
\addplot[variable=\m, thin, blue] {2*m+2};
% Works:
\addplot[variable=\m, only marks, red, samples at={0,1,...,7}] {2*m+2 };
% Works:
\addplot[variable=\m, densely dashed, mark=+, green, samples at={0,1,...,7}] {a(\m)};
\end{axis}
\end{tikzpicture}
\end{document}