有人知道为什么这个图在 0 和 1 之间看起来很奇怪吗?如果我尝试绘制立方根函数,也会发生同样的情况。请指教。我试图避免使用 pgfplots 包。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=.6,cap=round]
\tikzset{axes/.style={}}
% The graphic
\draw[style=help lines,step=1cm, dotted] (-.5,-3.5) grid (10.5,5.5);
\begin{scope}[style=axes]
\draw[->] (-0.5,0) -- (10.5,0) node[right] {$x$};
\draw[->] (0,-3.5) -- (0,5.5) node[above] {$y$};
\foreach \x/\xtext in {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
\draw[xshift=\x cm] (0pt,2.6pt) -- (0pt,-2.6pt) node[below,fill=white]
{\scriptsize $\xtext$};
\foreach \y/\ytext in {-3, -2, -1, 1, 2, 3, 4, 5}
\draw[yshift=\y cm] (2.6pt,0pt) -- (-2.6pt,0pt) node[left,fill=white]
{\scriptsize $\ytext$};
%\filldraw[blue] (0,0) circle (3pt) node[fill=white,below right=12pt]
{\scriptsize $(0,0)$};
\filldraw[blue] (1,1) circle (3pt) node[fill=white,above=3pt] {\scriptsize
$(1,1)$};
\filldraw[blue] (3,1.732) circle (3pt) node[fill=white,below=3pt]
{\scriptsize $(3,\sqrt{3})$};
\filldraw[blue] (4,2) circle (3pt) node[fill=white,above=3pt] {\scriptsize
$(4,2)$};
\filldraw[blue] (9,3) circle (3pt) node[fill=white,above=3pt] {\scriptsize
$(9,3)$};
\draw[domain=0:10,smooth,variable=\x,blue,thick,->] plot ({\x},{(\x)^.5});
\end{scope}
\end{tikzpicture}
\end{document}
答案1
因为您的样本不够大:默认情况下是 25 个点。如果样本为 200 个点,则此方法有效。
编辑:修改示例。非常感谢 Skillmon 提出修改示例的想法,也感谢 @marmot 给出一个简单有效的示例。
你的\foreach
循环不需要有两个变量。只需要一个就足够了,为了让显示的文本更小,这就是关键font=\scriptsize
。
编辑2:
为了避免对每个节点重复此操作,只需every node/.style={font=\scriptsize}
覆盖预先存在的样式或every node/append style={font=\scriptsize}
简单地将此样式添加到预先存在的样式而不覆盖它。
\documentclass{article}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}[scale=.6,cap=round,every node/.style={font=\scriptsize}]
\tikzset{axes/.style={}}
% The graphic
\draw[style=help lines,step=1cm, dotted] (-.5,-3.5) grid (10.5,5.5);
\begin{scope}[style=axes]
\draw[->] (-0.5,0) -- (10.5,0) node[right] {$x$};
\draw[->] (0,-3.5) -- (0,5.5) node[above] {$y$};
\foreach \x in {1, ..., 10}
\draw[xshift=\x cm] (0pt,2.6pt) -- (0pt,-2.6pt) node[below,fill=white]
{ \x};
\foreach \y in {-3,..., 5}
\draw[yshift=\y cm] (2.6pt,0pt) -- (-2.6pt,0pt) node[left,fill=white]
{ \y};
\filldraw[blue] (0,0) circle (3pt) node[fill=white,below right=12pt]
{$(0,0)$};
\filldraw[blue] (1,1) circle (3pt) node[fill=white,above=3pt] {
$(1,1)$};
\filldraw[blue] (3,1.732) circle (3pt) node[fill=white,below=3pt]
{$(3,\sqrt{3})$};
\filldraw[blue] (4,2) circle (3pt) node[fill=white,above=3pt] {
$(4,2)$};
\filldraw[blue] (9,3) circle (3pt) node[fill=white,above=3pt] {
$(9,3)$};
\draw[domain=0:10,smooth,variable=\x,blue,thick,->,samples at={0,0.05,...,1,1.5,...,10.5}] plot ({\x},{(\x)^.5});
\end{scope}
\end{tikzpicture}
\end{document}
答案2
@AndréC 答案的一个小变化(+1):再循环一次,计算标记位置,......
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=.6,
every node/.append style = {font=\scriptsize, inner sep=2pt, xscale=0.9}
]
% grid
\draw[help lines,dotted] (0,-3.5) grid (10.5,5.5);
% axis
\draw[->] (-0.4,0) -- (10.5,0) node[right] {$x$};
\draw[->] (0,-3.5) -- ( 0,5.5) node[above] {$y$};
\foreach \x in {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
\draw (\x,2pt) -- + (0,-4pt) node[below,fill=white] {\x};
\foreach \y in {-3,...,5}
\draw (2pt,\y) -- + (-4pt,0) node[left, fill=white] {\y};
% curve
\draw[smooth, blue,thick,->]
plot[samples at={0,0.05,...,1,1.5,...,10.5}] (\x,\x^.5);
% marks
\foreach \i/\j in {0/0, 1/1, 3/$\sqrt{3}$, 4/2, 9/3}
{ \pgfmathparse{sqrt(\i)}
\fill[blue] (\i,\pgfmathresult)
circle[radius=2pt] \ifnum\i>0 node[above=1pt] {(\i,\j)}\fi;
}
\end{tikzpicture}
\end{document}