对于使用 tikz 制作的图表来说,结果不太好

对于使用 tikz 制作的图表来说,结果不太好

我制作了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}

geogebra 图

答案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}

输出:

在此处输入图片描述

相关内容