我制作了y = \sqrt{|x|}
,在轴的开始处效果不太好。下面你可以看到 GeoGebra 的照片和这个 TeXlive.net 代码片段。
\documentclass[a4paper,10pt]{article}
\usepackage{pgf,tikz}
\begin{document}
\begin{tikzpicture}[scale=2]
\draw[very thick, ->] (-2.5,0) -- (2.5,0)node[pos=1,above]{$x$};
\draw[very thick, ->] (0,-1.2) -- (0,3)node[pos=1,left]{$y$};
\foreach \x in {-2,...,2}\draw[shift={(\x,0)},color=black]
(0pt,0.8pt) -- (0pt,-0.8pt);
\foreach \i in {1,2}
\draw[shift={(\i,0)},color=black](0pt,0pt) -- (0pt,-0.8pt)node[below]{$\i$};
\foreach \i in {-2,-1}
\draw[shift={(\i,0)},color=black](0pt,0pt) -- (0pt,-0.8pt)node[below]{$\i$};
\node[below right](o) at (0,0){$0$};
\draw[ultra thick, samples=100, draw=black, domain=-2.5:2.5] plot (\x,{sqrt(abs(\x))});
\end{tikzpicture}
\end{document}
答案1
这篇有趣的文章解释如何有效使用sample
。这给出了以下简单的解决方案。
\documentclass[a4paper,10pt]{article}
\usepackage{pgf,tikz}
\begin{document}
\begin{tikzpicture}[scale=2]
\draw[very thick, ->] (-2.5,0) -- (2.5,0)node[pos=1,above]{$x$};
\draw[very thick, ->] (0,-1.2) -- (0,3)node[pos=1,left]{$y$};
\foreach \x in {-2,...,2}\draw[shift={(\x,0)},color=black]
(0pt,0.8pt) -- (0pt,-0.8pt);
\foreach \i in {1,2}
\draw[shift={(\i,0)},color=black](0pt,0pt) -- (0pt,-0.8pt)node[below]{$\i$};
\foreach \i in {-2,-1}
\draw[shift={(\i,0)},color=black](0pt,0pt) -- (0pt,-0.8pt)node[below]{$\i$};
\node[below right](o) at (0,0){$0$};
% Use 2^n + 1 for sample.
% line join=bevel <-- See the quark67's comment.
\draw[ultra thick, samples=257, draw=black, domain=-2.5:2.5, line join=bevel] plot (\x,{sqrt(abs(\x))});
\end{tikzpicture}
\end{document}
答案2
我建议使用pgfplots
图表。这样更简单。
\documentclass[x11names,svgnames,11pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\centering
\begin{tikzpicture}
\begin{axis} [ width=12cm,
xlabel = $x$, ylabel = $y$,
]
\addplot [red,thick,domain=-3:3,samples=500,smooth]
{sqrt(abs(x))} ;
\end{axis}
\end{tikzpicture}
\end{document}
输出: