如何使用 pgfplots 创建这个甜甜圈/圆环饼图?

如何使用 pgfplots 创建这个甜甜圈/圆环饼图?

我想创建这个图表。我知道,我应该提供类似 MWE 的东西,但我不知道从哪里开始。我查看了 pgfplots 手册中的所有示例,但我没有看到任何可以作为这个图表的起点的图表。这可能吗?你能帮我创建它吗?谢谢!

甜甜圈饼图

答案1

在现有的解决方案中,我发现最接近您的屏幕截图的解决方案可能是这个答案。这里有一个方法有些相似的版本,但使用的是图片而不是宏。我发布它的原因是这个版本可以轻松转换(线宽也会转换,这就是为什么有这个神秘的\pgfgettransformentries...)。所以你只需要说

\path (0,0) pic[scale=5,donut/offset=90]{donut=26};

要得到

在此处输入图片描述

以下是包含此示例和其他示例的代码。我尝试包含几个选项。最近添加的选项是clockwisebar(针对 Stefan Pinnow)。`bar 选项的实现有点“麻烦”,因为我不想只画一条白线,如果背景不平凡,或者有人想调整不透明度,这会适得其反。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
% from https://tex.stackexchange.com/a/76216/121799
\tikzset{clip even odd rule/.code={\pgfseteorule}}
\begin{document}
    \begin{tikzpicture}[
        pics/donut/.style={
            code={
                \node[
                    font=\sffamily\bfseries,
                    text=\pgfkeysvalueof{/tikz/donut/fg},
                    transform shape,
                    circle,
                    /tikz/donut/donut node,
                ] (dn) {#1\%};
                \pgfgettransformentries{\myx}{\tmp}{\tmp}{\tmp}{\tmp}{\tmp}
                %
                \ifdim\pgfkeysvalueof{/tikz/donut/bar width}>0pt
                    \clip [clip even odd rule]  let
                        \p1=($(dn.north) - (dn.center)$),
                        \n1={veclen(\x1,\y1)},
                        \n2={\n1 + \pgfkeysvalueof{/tikz/donut/line width}}
                    in
                        (0,0) circle [radius=0.99*\n1]
                        (0,0) circle [radius=1.01*\n2]
                        ($(\pgfkeysvalueof{/tikz/donut/direction}*\pgfkeysvalueof{/tikz/donut/offset}:0.8*\n1)+({\pgfkeysvalueof{/tikz/donut/direction}*\pgfkeysvalueof{/tikz/donut/offset}+90}:\pgfkeysvalueof{/tikz/donut/direction}*\pgfkeysvalueof{/tikz/donut/bar width}/2)$)
                        -- ++
                        ({\pgfkeysvalueof{/tikz/donut/direction}*\pgfkeysvalueof{/tikz/donut/offset}}:2.5*\pgfkeysvalueof{/tikz/donut/line width})
                        -- ++
                        ({\pgfkeysvalueof{/tikz/donut/direction}*(\pgfkeysvalueof{/tikz/donut/offset}-90)}:\pgfkeysvalueof{/tikz/donut/bar width})
                        -- ++
                        ({\pgfkeysvalueof{/tikz/donut/direction}*(\pgfkeysvalueof{/tikz/donut/offset}+180)}:2.5*\pgfkeysvalueof{/tikz/donut/line width})
                        -- cycle
                        %
                        ($({\pgfkeysvalueof{/tikz/donut/direction}*(\pgfkeysvalueof{/tikz/donut/offset}+#1*3.6)}:0.8*\n1)+
                        ({\pgfkeysvalueof{/tikz/donut/direction}*(\pgfkeysvalueof{/tikz/donut/offset}+90+#1*3.6)}:\pgfkeysvalueof{/tikz/donut/bar width}/2)$)
                        -- ++
                        ({\pgfkeysvalueof{/tikz/donut/direction}*(\pgfkeysvalueof{/tikz/donut/offset}+#1*3.6)}:2.5*\pgfkeysvalueof{/tikz/donut/line width})
                        -- ++
                        ({\pgfkeysvalueof{/tikz/donut/direction}*(\pgfkeysvalueof{/tikz/donut/offset}+#1*3.6-90)}:\pgfkeysvalueof{/tikz/donut/bar width})
                        -- ++
                        ({\pgfkeysvalueof{/tikz/donut/direction}*(\pgfkeysvalueof{/tikz/donut/offset}+#1*3.6+180)}:2.5*\pgfkeysvalueof{/tikz/donut/line width})
                        -- cycle
                    ;
                \fi
                %
                \draw [
                    color=\pgfkeysvalueof{/tikz/donut/fg},
                    line width=\myx * \pgfkeysvalueof{/tikz/donut/line width}
                ] let
                    \p1=($(dn.north) - (dn.center)$),
                    \n1={
                        veclen(\x1,\y1) + 0.5*\pgfkeysvalueof{/tikz/donut/line width}
                    }
                in ({\pgfkeysvalueof{/tikz/donut/direction}*(
                    \pgfkeysvalueof{/tikz/donut/deficit}
                    + \pgfkeysvalueof{/tikz/donut/offset})}:\n1)
                arc ({\pgfkeysvalueof{/tikz/donut/direction}*(
                    \pgfkeysvalueof{/tikz/donut/deficit}
                    + \pgfkeysvalueof{/tikz/donut/offset})}:{
                    \pgfkeysvalueof{/tikz/donut/direction}*(3.6*#1
                    + \pgfkeysvalueof{/tikz/donut/offset}
                    - \pgfkeysvalueof{/tikz/donut/deficit})}:\n1);
                \draw[
                    color=\pgfkeysvalueof{/tikz/donut/bg},
                    line width=\myx*\pgfkeysvalueof{/tikz/donut/line width}
                ] let
                    \p1=($(dn.north) - (dn.center)$),
                    \n1={
                        veclen(\x1,\y1) + 0.5*\pgfkeysvalueof{/tikz/donut/line width}
                    }
                in ({\pgfkeysvalueof{/tikz/donut/direction}*(
                    3.6*#1
                    +\pgfkeysvalueof{/tikz/donut/deficit}
                    +\pgfkeysvalueof{/tikz/donut/offset})}:\n1
                )
                arc ({\pgfkeysvalueof{/tikz/donut/direction}*(3.6*#1
                    + \pgfkeysvalueof{/tikz/donut/deficit}
                    + \pgfkeysvalueof{/tikz/donut/offset})}:{
                    \pgfkeysvalueof{/tikz/donut/direction}*(360
                    + \pgfkeysvalueof{/tikz/donut/offset}
                    - \pgfkeysvalueof{/tikz/donut/deficit})}:\n1
                );
            },
        },
        donut/.cd,
            fg/.initial=orange,
            bg/.initial=gray!40,
            line width/.initial=3mm,
            deficit/.initial=2,
            donut node/.style={},
            offset/.initial=0,
            direction/.initial=1,
            bar width/.initial=0pt,
            clockwise/.code=\tikzset{donut/direction=-1},
            bar/.code=\tikzset{donut/deficit=0pt,donut/bar width=#1}
    ]
        \path
            (0,0)  pic [scale=5,donut/offset=90]                {donut=26}
            (8,0)  pic [scale=3,donut/clockwise]                {donut=87}
            (0,-8) pic [scale=3,donut/.cd,fg=blue,bg=yellow,
                        donut node/.style={inner sep=1pt}]      {donut=38}
            (8,-8) pic [scale=3,donut/.cd,
                        fg=red,bg=yellow,clockwise,
                        bar=1mm, offset=200]     {donut=38.7346}
            (0,-16) pic [scale=3,donut/.cd, clockwise,bar=1mm,offset=270]   {donut=56}                     
            (8,-16) pic [scale=3,donut/.cd,bar=1mm, offset=200]      {donut=76}                     
        ;
  \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

wheelchart包裹:

\documentclass[tikz, border=2mm]{standalone}
\usepackage{wheelchart}
\usepackage[detect-all]{siunitx}
\usepackage{lmodern}

\begin{document}

\def\n{26}
\begin{tikzpicture}[font=\sffamily\bfseries]
\wheelchart[
data={},
gap polar,
middle={\Huge\qty{\n}{\percent}},
]{\n/orange,{100-\n}/gray}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容