答案1
对于潜在的部分修复的一些提示:
- 选择奇数样本大小(例如
201
而不是200
)似乎有点帮助 - pgfPlots 似乎在计算方面做得更好一些,并且在几乎所有其他绘图方面也比原始 TikZ 做得更好。
如果要绘制的函数太复杂,则可以:
- 难以计算
- 需要大量样本点
- 需要较高的浮点精度
- 需要不规则微调采样点间距
那么最好在 TeX 之外预先计算数据,例如使用 python 或 (Mat-)Scilab/Octave 或 gnuPlot,然后将数据作为外部文件输入到 pgfPlots
另外,据报道 LuaLaTeX 对于计算很有用,但对我来说示例有点少。
输出
代码
\documentclass[12pt,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
height=10cm,
unit vector ratio = 1 1,
domain=-1:1,samples=41,
axis lines = center,
enlarge y limits=.2,
]
% your function is hard to plot near its vertical tangents
\addplot [line width=1mm,blue,samples=201] {sqrt(1-x*x)} node [pos=.5, anchor= south west] {201 sample points};
\addplot [thick,green!80!black,] {sqrt(1-x*x)} node [pos=.5, anchor= south east] {41 sample points};
% much easier to plot
\addplot [thick,red,domain=-180:0] ({cos(x)},{sin(x)}) node [pos=.5, anchor= north west] {41 sample points};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
如果使用 PGFPlots 绘制函数也是可以的,那么使用该data cs=polar
选项来实现这一点真的很容易。
% used PGFPlots v1.15
% adapted from <https://tex.stackexchange.com/a/338915>
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xmin=-1.5, xmax=1.5,
ymin=-1.5, ymax=1.5,
xlabel=$x$,
ylabel=$y$,
smooth,
% % if you want the (half) circles to look like
% % circles instead of ellipses ...
% axis equal,
]
% -----------------------------------------------------------------
% use `data cs=polar' either here, so it is only used for that plot
% or add it as axis option so all `\addplot' commands use this
% coordinate system
% (all TikZ stuff still uses normal Cartesian coordinate system)
\addplot+ [data cs=polar,domain=0:180] {1.25};
% -----------------------------------------------------------------
% just do demonstrate that the above mentioned is true
\addplot+ [domain=-1.25:1.24] {x};
\end{axis}
\end{tikzpicture}
\end{document}
答案3
TikZ\foreach
与定点操作混淆了。
\documentclass{article}
\usepackage{pgffor}
\begin{document}\parindent0pt
\foreach\x in {0.8,0.84,...,1}{\x, }
\foreach\x in {0.8,0.88,...,1}{\x, }
\foreach\x in {0.9,0.92,...,1}{\x, }
\foreach\x in {0.9,0.91,...,1}{\x, }
\end{document}
请注意,TikZ 仅检查下一个点是否超出给定的终点,以作为停止标准。它不会进行调整以使终点包含在列表中。