我想画一个圆并显示一些分数。我为所有部分都具有相同大小和颜色的情况编写了一个代码。但如果我想让各个部分彼此不同(即大小和颜色)怎么办?
\documentclass[tikz, border=5]{standalone}
\newcount\segmentsleft
\tikzset{pics/.cd,
circle fraction/.style args={#1/#2}{code={%
\segmentsleft=#1\relax
\pgfmathloop
\ifnum\segmentsleft<1\else
\ifnum\segmentsleft<#2 \edef\n{\the\segmentsleft}\else\def\n{#2}\fi
\begin{scope}[shift={(\pgfmathcounter,0)}]
\foreach \i [evaluate={\a=360/#2*(\i-1)+90;}] in {1,...,\n}
\fill[fill=purple,line width =1pt] (0,0) -- (\a:3/8) arc (\a:\a+360/#2:3/8) -- cycle;
\draw circle [line width =1pt,radius=3/8];
\ifnum#2>1
\foreach \i [evaluate={\a=360/#2*(\i-1);}] in {1,...,#2}
\draw (0,0) -- (90+\a:3/8);
\fi
\end{scope}
\advance\segmentsleft by-#2
\repeatpgfmathloop
}}
}
\begin{document}
\begin{tikzpicture}[line width=0.1pt]
\foreach \numerator/\denominator [count=\y]
in {5/8}{
% \node at (-1/2,-\y) {$\frac{\numerator}{\denominator}$};
\pic at (0, -\y) {circle fraction={\numerator/\denominator}};
}
\end{tikzpicture}
\end{document}
答案1
像这样:
这可以简单地在一个循环中绘制,而无需定义pic
饼图部分的图像:
\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \i/\j/\k in {0/45/white,45/90/yellow,90/135/orange,135/150/red,
150/210/purple, 210/270/blue, 270/300/teal,300/360/white}
{
\draw[fill=\k] (0,0) -- (\i:2) arc (\i:\j:2);
}
\end{tikzpicture}
\end{document}