latextikzpgf

TikZ: Fill a circle with shading color by angle


Goal: Draw a semi-circle and fill it with line-gradient color that changes according to angle. For example, with angle from θ=0 to θ=π, it is expected that the filling color shades from red to blue.

In tikz manual (https://tikz.dev/library-shadings), the shading pattern does not statisfy my need in the following aspects:

  1. The option shading = color wheel seems to only applicable to a circle rather than semi-circle.

  2. The option shading = color wheel renders a distorted color at the origin, while shading=color wheel white center fills a white region at the origin.

  3. The color whell seems not really line-gradient.

So, is there any way to make an ideal semi-circle with line-gradient color filling by angle?


A similar example can be seen here, but some changes are needed:

  1. It is expected to be semi-circle rather than a quadrant.

  2. The filled color is prefered to be gradient according the angle(or its consine).


I tried the following codes, but the gradient pattern seems not quite obvious, and the origin is screwy.

\documentclass[tikz, margin=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{shadings}

\begin{document}

\begin{tikzpicture}
    \clip (0, 0) -- (1, 0) arc [start angle=0, end angle=180, radius=1cm] -- cycle ;
    \shade[shading=color wheel, shading angle=-60] (0, 0) circle (1cm) ;
\end{tikzpicture}

\end{document}

Solution

  • I suggest to use pgfplots, then you have the most control over the colour scale:

    \documentclass[tikz, margin=5pt]{standalone}
    
    \usepackage{pgfplots}
    \pgfplotsset{compat=newest}
    
    \begin{document}
    
    \begin{tikzpicture}
    \begin{axis}[
        axis equal=true,
        hide axis,     
        domain=0:180,
        domain y=0:1,
        view={0}{90},
        shader=interp,
        colormap={redblue}{
            color=(red)  color=(blue)
        },
        data cs=polar,
    ]
        \addplot3 [surf] {x};
    \end{axis}
    \end{tikzpicture}
    
    \end{document}
    

    enter image description here