我想绘制一个简单的圆圈,但是这段代码让我非常痛苦。以下是我写的:
\begin{figure}
\centering
\begin{tikzpicture}
\pgfplotsset{ticks=none}
\begin{axis}[
axis lines = left,
xlabel = $x$,
ylabel = {$f(x)$},
yticklabels={,,}
]
%Below the red parabola is defined
\addplot [
domain=-10:10,
samples=100,
color=red,
]
{x^2 +y^2 =1};
\end{axis}
\end{tikzpicture}
\caption{Predicted graph for for position versus time.}
\label{fig:my_label}
\end{figure}
但每次我尝试编译时,它都会抛出异常。我也尝试使用
\addplot [
domain=-10:10,
samples=100,
color=red,
]
{\sqrt{1-x^2}};
但无济于事。什么都不起作用。任何帮助都将不胜感激。如何使用 pgfplots 绘制一个圆圈(以及半个圆圈)?
答案1
有什么理由不简单地使用circle
TikZarc
吗?
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest, ticks=none}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xmin=-11, xmax=11, ymin=-11, ymax=11,
axis equal,
xlabel = $x$,
ylabel = {$f(x)$},
yticklabels={,,}
]
\draw (axis cs: 0, 0) circle [radius=10];% I've set the radius to 10 only for better show the image
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xmin=-11, xmax=11, ymin=-11, ymax=11,
axis equal,
xlabel = $x$,
ylabel = {$f(x)$},
yticklabels={,,}
]
\draw (axis cs: 10,0) arc[radius =10, start angle= 0, end angle= 180];
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案2
您可以使用极坐标并简单地绘制 sin(x)。
\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar}
\begin{document}
\begin{tikzpicture}
\begin{polaraxis}[
domain = 0:180,
samples = 100,
axis lines = none,
]
\addplot[thick] {sin(x)};
\end{polaraxis}
\end{tikzpicture}
\end{document}
答案3
第一次尝试会抛出错误,因为它不知道如何绘制隐函数(特别是,语法 会让人感到困惑=
)。第二次尝试是正确的,只是你应该使用sqrt
而不是\sqrt
。稍作修改的命令
\addplot[domain=-1:1, samples=100, color=red] {sqrt(1-x^2)};
应该可以绘制半圆。(请注意,正确的域对于获取半圆的端点很重要。)
对于整个圆,您可以使用参数化,而不是给出隐式函数:
\documentclass[tikz,border=3pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = $x$,
ylabel = {$f(x)$},
yticklabels={,,}
]
\addplot [domain=-180:180, samples=100, color=red] ({cos(x)},{sin(x)});
\end{axis}
\end{tikzpicture}
\end{document}
(我希望能够清楚地了解如何修改它以得到半圆。)
答案4
%%%%%%%%%%%%%%%%%%%
\begin{center}
\begin{picture}(100,100)(0,0)
\setlength{\unitlength}{1pt}
\put(20,70){\circle{30}} \put(20,70){\circle*{10}} % left eye
\put(80,70){\circle{30}} \put(80,70){\circle*{10}} % right eye
\put(40,40){\line(1,2){10}} \put(60,40){\line(-1,2){10}} \put(40,40){\line(1,0){20}} % nose
\put(50,20){\oval(80,10)[b]} % mouth
\multiput(0,90)(4,0){10}{\line(1,3){4}} % left eyebrow
\multiput(100,90)(-4,0){10}{\line(-1,3){4}} % right eyebrow
\end{picture}
\end{center}
请查看这里有更多更好的例子,更简约,适合教育目的: https://github.com/spartrekus/Latex-tfig-resource-figure