请帮我提供一些如何使用 tikz 创建弧形图表的提示。我尝试过使用以下方法,但我无法继续
\documentclass[border=4mm]{standalone}
\usepackage{tikz}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%for arc and angles
\usetikzlibrary{calc, angles}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{figure}[H]
\begin{tikzpicture}
\foreach \iangle in {35,45,55,70,80,90,105,115,130};
\draw[green] ([shift={(0,0)}]145:1) arc[radius=9,start angle=30,end
angle= 150];
\draw (0.3,0) arc (0:30:0.3) node at (15:0.5) {$\varphi$};
\end{tikzpicture}
\caption{Caption1}
\label{fig3}
\end{figure}
\end{document}
答案1
当你在等待 TikZ 的指导时,这里有一个尝试元帖子,展示了循环结构和内联构造的一些特性if..fi
以及你可以用suffix
变量做什么。我很喜欢这个,我希望它至少可以作为可以做什么的一个例子。
\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
path C, C';
C = subpath(2,0) of fullcircle rotated 45 scaled 640;
C' = C scaled 1.02;
numeric p[];
p1 = 1/8;
p2 = 2/8;
p3 = 3/8;
p.i = 5/8;
p.ii = 6/8;
p.iii = 7/8;
p.n = 9/8;
p.l = 10/8;
p.zz = 13/8;
for i= 1/64 step 1/64 until 3/64: drawdot point i of C' withpen pencircle scaled 1; endfor
for i= 29/64 step 1/64 until 35/64: drawdot point i of C' withpen pencircle scaled 1; endfor
for i= 61/64 step 1/64 until 67/64: drawdot point i of C' withpen pencircle scaled 1; endfor
for i= 89/64 step 1/64 until 95/64: drawdot point i of C' withpen pencircle scaled 1; endfor
for i=116/64 step 1/64 until 119/64: drawdot point i of C' withpen pencircle scaled 1; endfor
forsuffixes $=1,2,3,i,ii,iii,n,l,zz:
draw point p$ of C -- point p$ of C' withcolor 2/3 blue;
label.top("$X_{" &
if str $ = "ii" : "i+1"
elseif str $ = "iii": "i+2"
elseif str $ = "zz" : "n+l-1"
else: str $ fi
& "}$", point 1 of C') rotated (45-45*p$);
endfor
draw C;
vardef mark(suffix a, b)(expr s, T) =
save arc; path arc;
arc = subpath(p.a, p.b) of C scaled s;
interim ahangle := 180; interim ahlength := 5;
drawdblarrow arc withcolor 2/3 green;
save pp; picture pp;
pp = thelabel(T rotated angle direction 1/2 of arc, point 1/2 of arc);
unfill bbox pp; draw pp;
enddef;
mark(1, i, 0.95, textext("$Y_1$"));
mark(2, ii, 0.90, textext("$Y_2$"));
mark(3, iii, 0.85, textext("$Y_3$"));
mark(n, zz, 0.95, textext("$Y_n$"));
endfig;
\end{mplibcode}
\end{document}
这是包含在内luamblib
以便编译lualatex
,或适应普通 MP,或gmp
包等。
笔记
我将其定义
C
为通常fullcircle
路径的四分之一,旋转 45° 并反转,以便它顺时针运行,或者从左到右运行。这条路径
C
的长度为两个“时间”单位。因此,点 0 是左端,点 1 是中间,点 2 是右端。路径
C'
相同,但略微扩大。该
p[]
部分定义了我们想要的各个时间C
。前五个循环绘制了一些点。如果这真的很聪明,我会将所有这些值与我刚刚在中定义的值联系起来
p...
,但事实并非如此。点线可能看起来是直的,但实际上它们是弯曲的。然后,大部分艰苦的工作都由循环完成
forsuffixes
。使用字母和数字作为后缀很方便,但也有限制,所以不能将整个表达式作为后缀。因此,为了获得“i+1”等,我使用了一些内联来if
测试它们。不是很灵活,但对其中一些来说已经足够好了。然后用内联宏完成绿线
mark
。
答案2
在 TikZ 中,圆弧的起点用 给出\draw
,而不是圆心。但对于同心圆弧,必须知道圆心。为此\coordinate
使用 a,然后使用起始角度和半径计算圆弧的起点。可以使用相同的方法计算径向线的起点和终点。
\begin{tikzpicture}
\coordinate (C) at (0,0); % center point
\draw[red] (C) circle (0.05) node[right] {center}; % mark the center, just to show it
\draw (C) arc (30:60:1) node[above] {arc starting at center}; % arc starting at center, just to show how an arc is drawn an arc is drawn
\draw[green] ($(C) + (30:9)$) arc (30:150:9); % main arc
% partial arcs and radial lines
\draw ($(C) + (120:8.6)$) arc (120:90:8.6) node[fill=white,inner sep=1pt] at (105:8.6) {$\varphi$};
\draw ($(C) + (120:8.5)$) -- ($(C) + (120:9)$);
\draw ($(C) + (90:8.5)$) -- ($(C) + (90:9)$);
\end{tikzpicture}
这可以在\foreach
循环中自动完成。此处的列表中给出了起始角度、终止角度和到主弧的距离。然后计算其他值。对于标签,使用count
选项。\foreach
完整代码:
\documentclass[border=4mm]{standalone}
\usepackage{tikz}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%for arc and angles
\usetikzlibrary{calc}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{tikzpicture}
\coordinate (C) at (0,0); % center point
\pgfmathsetmacro\overlength{0.1} % additional length for radial lines
\pgfmathsetmacro\ticklength{0.2} % length of radial tick
\pgfmathsetmacro\mainradius{9}
\draw[green] ($(C) + (30:9)$) arc (30:150:\mainradius); % main arc
\foreach \startangle/\endangle/\dist[count=\cnt] in {120/90/0.4,110/80/0.8}{%
\pgfmathsetmacro\midangle{(\startangle+\endangle)/2} % angle for node
\pgfmathsetmacro\arcradius{\mainradius-\dist} % radius
\pgfmathsetmacro\startline{\arcradius-\overlength} % start of radial line
\pgfmathsetmacro\tickend{\mainradius+\ticklength} % end of tick
\pgfmathsetmacro\cnti{int(\cnt-1)} % count for $X_l$
% arc
\draw ($(C) + (\startangle:\arcradius)$) arc (\startangle:\endangle:\arcradius) node[fill=white,inner sep=1pt] at (\midangle:\arcradius) {$Y_\cnt$};
% radial lines
\draw ($(C) + (\startangle:\startline)$) -- ($(C) + (\startangle:\mainradius)$);
\draw ($(C) + (\endangle:\startline)$) -- ($(C) + (\endangle:\mainradius)$);
% ticks
\draw ($(C) + (\startangle:\mainradius)$) -- ($(C) + (\startangle:\tickend)$) node[above] {$X_\cnt$};
\draw ($(C) + (\endangle:\mainradius)$) -- ($(C) + (\endangle:\tickend)$) node[above] {$\ifnum\cnti=0\relax X_{l}\else X_{l+\cnti}\fi$};
}
% drawing dots
\draw[loosely dotted, very thick] ($(C) + (105:9.15)$) arc (105:95:9.15);
\end{tikzpicture}
\end{document}