我怎样才能使用 tikz 为环填充颜色?

我怎样才能使用 tikz 为环填充颜色?

我想为半径为 r_1 和 r_3 的圆之间的环着色,但保留圆的线条和线条 r_1、r_2、r_3。我尝试了颜色填充,但线条消失了。在此处输入图片描述

这是我的 tikz 代码:

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw[->,color=black, thick] (-3.66,0) -- (3.66,0);      
\draw[shift={(\x,0)},color=black] (0pt,-2pt);    
\draw[->,color=black, thick] (0,-3.66) -- (0,3.66);    
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);    
\clip(-3.66,-4.4) rectangle (4.65,4.65);    

\draw [name path=A, thick, draw=black](0,0) circle (1cm);    
\draw [name path=B, thick, draw=black](0,0) circle (2cm);    
\draw [name path=C, thick, draw=black](0,0) circle (1.5cm);    
\draw [thick, draw=black](0,0)-- (0.76,0.64);    
\draw [thick, draw=black](0,0)-- (1.13,1.65);    
\draw [thick, draw=black](0,0)-- (0.37,1.45);    

\begin{scriptsize}    
\draw[color=black] (-1.54,1.94) node {$R$};    
\draw[color=black] (1.11,0.73) node {$r_1$};    
\draw[color=black] (1.48,1.81) node {$r_3$};    
\draw[color=black] (0.55,1.69) node {$r_2$};    
\end{scriptsize}    

\end{tikzpicture}

答案1

事物是按照定义顺序绘制的,因此,如果先绘制线条,然后填充,则填充将覆盖线条。要仅填充内圆和外圆之间的区域,您可以在图表的最开始处(紧接着\begin{tikzpicture}[...])添加以下内容

\fill [red,even odd rule] (0,0) circle[radius=2cm] circle[radius=1cm];

在下面的代码中,我介绍了一些绘制图表的其他技巧。圆圈是使用循环绘制的,r_N标签附加在径向线的末端,径向线本身是使用极坐标绘制的(<angle>:<radius>)

我没有包含这些name path内容,因为您似乎没有将其用于任何用途,并且我删除了x=1cm,y=1cmcolor=black,因为无论如何它们都是默认设置。我还用from替换了库triangle 45中的箭头,因为我认为前一个库已被弃用,而后者则被弃用。arrowsTrianglearrows.metapgf

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[
  line cap=round,
  line join=round,
  >=Triangle,
  myaxis/.style={->,thick}
]

\fill [red,even odd rule] (0,0) circle[radius=2cm] circle[radius=1cm];

\foreach \radius in {1,1.5,2}
  \draw [thick] (0,0) circle[radius=\radius cm];

\begin{scope}[
  every node/.append style={
    circle,
    font=\scriptsize,
    inner sep=1pt
}]
   \node at (135:2.5cm) {$R$};

   % The following \foreach does the same as the three \draws immediately below
   %\foreach [count=\i] \Ang/\Rad in {40/1,75/1.5,55/2}
   %   \draw [thick] (0,0) -- (\Ang:\Rad cm) node[anchor=180+\Ang] {$r_{\i}$};

   \draw [thick] (0,0) -- (40:1cm) node[anchor=220] {$r_1$};
   \draw [thick] (0,0) -- (55:2cm) node[anchor=235] {$r_3$};
   \draw [thick] (0,0) -- (75:1.5cm) node[anchor=255] {$r_2$};
\end{scope}

\draw[myaxis] (-3.66,0) -- (3.66,0);      
\draw[myaxis] (0,-3.66) -- (0,3.66);    

\end{tikzpicture}
\end{document}

答案2

像这样吗?画圆和画线的顺序很重要。另外,你不必说draw=black,这是默认的。

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]

\draw [name path=B, thick, draw,fill=blue!20](0,0) circle (2cm);    
\draw [name path=C, thick, draw](0,0) circle (1.5cm); 
\draw [name path=A, thick, draw,fill=white](0,0) circle (1cm);   
\draw [thick](0,0)-- (0.76,0.64);    
\draw [thick](0,0)-- (1.13,1.65);    
\draw [thick](0,0)-- (0.37,1.45);   

\draw[->, thick] (-3.66,0) -- (3.66,0);      
\draw[->, thick] (0,-3.66) -- (0,3.66); 

\begin{scriptsize}    
\draw (-1.54,1.94) node {$R$};    
\draw (1.11,0.73) node {$r_1$};    
\draw (1.48,1.81) node {$r_3$};    
\draw (0.55,1.69) node {$r_2$};    
\end{scriptsize}    

\end{tikzpicture}

在此处输入图片描述

相关内容