我尝试循环pgfplots
绘制曲线下方的矩形,但不起作用。这是我的代码
\begin{tikzpicture}\begin{axis}[
width=160pt,compat=1.5.1,grid style={ultra thin},
x tick label style={font=\tiny},y tick label style={font=\tiny},
scale only axis,grid=major,axis lines=middle,
xlabel={$x$},
ylabel={$y$},
xmin=-0.13,
xmax=1.6,
domain=-0.25:2,
ymin=-0.3,
ymax=3.3,
xtick={0,0.25,...,1.5}, xticklabels={,,0.5,,1,,1.5},
ytick={0,0.5,...,3},
legend style={at={(0.5,-0.05)},anchor=north,nodes={right}},
extra tick style={tickwidth=2pt},
extra x ticks={1.1},
extra x tick labels={$ 1.1 $},
]
\addplot [red,thick,samples=200] { sin(deg(x^2)) };
\addlegendentry{$ g(x)=\sin\left(x^2\right) $}
\foreach \n [evaluate={\y=sin(deg((\n+0.05)^2))}] in {0,0.05,...,1}
\draw (axis cs: \n, 0) rectangle (axis cs: \n+0.05, \y);
\end{axis}
\end{tikzpicture}
我做错了什么?
答案1
tikz
我无法解释为什么你的代码无法编译,因为对和了解不够(或者说根本不了解)pgfplots
。但是这个变体有效
\documentclass[tikz,border=12pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}[domain=-0.1:1.1]
\begin{axis}[
width=160pt,compat=1.5.1,grid style={ultra thin},
x tick label style={font=\tiny},y tick label style={font=\tiny},
scale only axis,grid=major,axis lines=middle,
xlabel={$x$},
ylabel={$y$},
xmin=-0.13,
xmax=1.6,
domain=-0.25:2,
ymin=-0.3,
ymax=3.3,
xtick={0,0.25,...,1.5}, xticklabels={,,0.5,,1,,1.5},
ytick={0,0.5,...,3},
legend style={at={(0.5,-0.05)},anchor=north,nodes={right}},
extra tick style={tickwidth=2pt},
extra x ticks={1.1},
extra x tick labels={$ 1.1 $},
]
\addplot [red,thick,samples=200] { sin(deg(x^2)) };
\addlegendentry{$ g(x)=\sin\left(x^2\right) $}
\foreach \n [evaluate={\y=sin(deg((\n+0.05)^2))}] in {0,0.05,...,1}
{%
\edef\x {\noexpand \draw (axis cs: \n, 0) rectangle (axis cs: \n+0.05, \y)}%
\x;
}
\end{axis}
\end{tikzpicture}
\end{document}