如何在 Tikz 中制作饼图?

如何在 Tikz 中制作饼图?

我尝试将两个饼图并排放置,但它们重叠了。

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz}

\begin{document}

\begin{figure}
  \begin{subfigure}{0.45\textwidth}
  \centering
%\adjustbox{trim=0 .01\height{} 0 .07\height{}, clip, width=.91\linewidth}
%  \includegraphics[page=1, width=\textwidth]{{sinus.gender}.pdf}
\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[nodes = {font=\sffamily}]
  \foreach \percent/\name in {
      72/Female (13),
         28/Male (5)
    } {
      \ifx\percent\empty\else               % If \percent is empty, do nothing
        \global\advance\cyclecount by 1     % Advance cyclecount
        \global\advance\ind by 1            % Advance list index
        \ifnum3<\cyclecount                 % If cyclecount is larger than list
          \global\cyclecount=0              %   reset cyclecount and
          \global\ind=0                     %   reset list index
        \fi
        \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
        \edef\color{\pgfmathresult}         %   and store as \color
        % Draw angle and set labels
        \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
          arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
        \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
        \node[pin=\angle+0.5*\percent*3.6:\name]
          at (\angle+0.5*\percent*3.6:\radius) {};
        \pgfmathparse{\angle+\percent*3.6}  % Advance angle
        \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
\end{tikzpicture}
  \caption{Sinus.}
  \end{subfigure}
  \begin{subfigure}{0.45\textwidth}
  \centering
%\adjustbox{trim=0 .02\height{} 0 .01\height{}, clip, width=.91\linewidth}
%  \includegraphics[width=\textwidth, page=1]{{arr.gender}.pdf}

\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[nodes = {font=\sffamily}]
  \foreach \percent/\name in {
      47/Female (22),
        53/Male (25)
    } {
      \ifx\percent\empty\else               % If \percent is empty, do nothing
        \global\advance\cyclecount by 1     % Advance cyclecount
        \global\advance\ind by 1            % Advance list index
        \ifnum3<\cyclecount                 % If cyclecount is larger than list
          \global\cyclecount=0              %   reset cyclecount and
          \global\ind=0                     %   reset list index
        \fi
        \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
        \edef\color{\pgfmathresult}         %   and store as \color
        % Draw angle and set labels
        \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
          arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
        \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
        \node[pin=\angle+0.5*\percent*3.6:\name]
          at (\angle+0.5*\percent*3.6:\radius) {};
        \pgfmathparse{\angle+\percent*3.6}  % Advance angle
        \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
\end{tikzpicture}
\end{subfigure}

\end{figure}

\end{document}

输出

在此处输入图片描述

\input如果能够将tikz 文档添加到我的代码中,那真是太棒了,\include这样我就不需要将所有代码都保存在一个文档中了。

测试 hhh 的字幕

% http://tex.stackexchange.com/questions/127227/subcaption-having-two-subfigures-captions-on-the-same-line
\begin{figure}
   \centering
   \subcaptionbox{Sinus}%
     [.4\linewidth]{\firstPic}}
    \subcaptionbox{Arr.}%
    [.4\linewidth]{\secondPic}
\caption{Gender proportions in populations.}
\label{figure:gender}
\end{figure}

输出

在此处输入图片描述

TeXLive:2016年
操作系统:Debian 8.5

答案1

我会选择 Tikz,这是它的混音版这里以下。

在此处输入图片描述

\documentclass{article}
\usepackage{graphicx}  
\usepackage{tikz}

\begin{document}

\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[nodes = {font=\sffamily}]
  \foreach \percent/\name in {
      72.2/Female (13),
        27.8/Male (5)
    } {
      \ifx\percent\empty\else               % If \percent is empty, do nothing
        \global\advance\cyclecount by 1     % Advance cyclecount
        \global\advance\ind by 1            % Advance list index
        \ifnum3<\cyclecount                 % If cyclecount is larger than list
          \global\cyclecount=0              %   reset cyclecount and
          \global\ind=0                     %   reset list index
        \fi
        \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
        \edef\color{\pgfmathresult}         %   and store as \color
        % Draw angle and set labels
        \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
          arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
        \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
        \node[pin=\angle+0.5*\percent*3.6:\name]
          at (\angle+0.5*\percent*3.6:\radius) {};
        \pgfmathparse{\angle+\percent*3.6}  % Advance angle
        \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
\end{tikzpicture}



\end{document}

还有其他可能性,例如pgf-pie通过此方式在 Google Code 中打包这里

\begin{tikzpicture}
    \pie{10/A, 20/B, 30/C, 40/D}
\end{tikzpicture}

有关的


并排显示两个饼图

首先,使用宏。然后使用showframe包来查看你正在玩什么。

在此处输入图片描述

因此我们将缩放 Tikz 图片

在此处输入图片描述

然后我们将用来hfill在两张图片之间添加一些额外的空间,成功!

在此处输入图片描述

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz}
%\usepackage{showframe}


\newcommand{\firstPic}{
\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[scale=0.45,nodes = {font=\sffamily}]
  \foreach \percent/\name in {
      72/Female (13),
         28/Male (5)
    } {
      \ifx\percent\empty\else               % If \percent is empty, do nothing
        \global\advance\cyclecount by 1     % Advance cyclecount
        \global\advance\ind by 1            % Advance list index
        \ifnum3<\cyclecount                 % If cyclecount is larger than list
          \global\cyclecount=0              %   reset cyclecount and
          \global\ind=0                     %   reset list index
        \fi
        \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
        \edef\color{\pgfmathresult}         %   and store as \color
        % Draw angle and set labels
        \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
          arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
        \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
        \node[pin=\angle+0.5*\percent*3.6:\name]
          at (\angle+0.5*\percent*3.6:\radius) {};
        \pgfmathparse{\angle+\percent*3.6}  % Advance angle
        \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
\end{tikzpicture}
}
\newcommand{\secondPic}{
\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[scale=0.45,nodes = {font=\sffamily}]
  \foreach \percent/\name in {
      47/Female (22),
        53/Male (25)
    } {
      \ifx\percent\empty\else               % If \percent is empty, do nothing
        \global\advance\cyclecount by 1     % Advance cyclecount
        \global\advance\ind by 1            % Advance list index
        \ifnum3<\cyclecount                 % If cyclecount is larger than list
          \global\cyclecount=0              %   reset cyclecount and
          \global\ind=0                     %   reset list index
        \fi
        \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
        \edef\color{\pgfmathresult}         %   and store as \color
        % Draw angle and set labels
        \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
          arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
        \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
        \node[pin=\angle+0.5*\percent*3.6:\name]
          at (\angle+0.5*\percent*3.6:\radius) {};
        \pgfmathparse{\angle+\percent*3.6}  % Advance angle
        \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
\end{tikzpicture}
}



\begin{document}

\begin{figure}
  \centering
  \begin{subfigure}{0.45\textwidth}
    \centering
    \firstPic
  \caption{Frist picture}
  \end{subfigure}
  \hfill
  \begin{subfigure}{0.45\textwidth}
    \centering
    \secondPic
  \caption{Second picture}
  \end{subfigure}

\caption{MAIN caption}
\end{figure}

\end{document}

子标题框正确对齐子图子标题

第一个是带有子图的,另一个是带有子标题框的。

在此处输入图片描述

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz}
%\usepackage{showframe}


\newcommand{\firstPic}{
\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[scale=0.45,nodes = {font=\sffamily}]
  \foreach \percent/\name in {
      72/Female (13),
         28/Male (5)
    } {
      \ifx\percent\empty\else               % If \percent is empty, do nothing
        \global\advance\cyclecount by 1     % Advance cyclecount
        \global\advance\ind by 1            % Advance list index
        \ifnum3<\cyclecount                 % If cyclecount is larger than list
          \global\cyclecount=0              %   reset cyclecount and
          \global\ind=0                     %   reset list index
        \fi
        \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
        \edef\color{\pgfmathresult}         %   and store as \color
        % Draw angle and set labels
        \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
          arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
        \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
        \node[pin=\angle+0.5*\percent*3.6:\name]
          at (\angle+0.5*\percent*3.6:\radius) {};
        \pgfmathparse{\angle+\percent*3.6}  % Advance angle
        \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
\end{tikzpicture}
}
\newcommand{\secondPic}{
\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[scale=0.45,nodes = {font=\sffamily}]
  \foreach \percent/\name in {
      47/Female (22),
        53/Male (25)
    } {
      \ifx\percent\empty\else               % If \percent is empty, do nothing
        \global\advance\cyclecount by 1     % Advance cyclecount
        \global\advance\ind by 1            % Advance list index
        \ifnum3<\cyclecount                 % If cyclecount is larger than list
          \global\cyclecount=0              %   reset cyclecount and
          \global\ind=0                     %   reset list index
        \fi
        \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
        \edef\color{\pgfmathresult}         %   and store as \color
        % Draw angle and set labels
        \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
          arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
        \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
        \node[pin=\angle+0.5*\percent*3.6:\name]
          at (\angle+0.5*\percent*3.6:\radius) {};
        \pgfmathparse{\angle+\percent*3.6}  % Advance angle
        \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
\end{tikzpicture}
}



\begin{document}

\begin{figure}
  \centering
  \begin{subfigure}{0.45\textwidth}
    \centering
    \firstPic
  \caption{Frist subfigure}
  \end{subfigure}
  \hfill
  \begin{subfigure}{0.45\textwidth}
    \centering
    \secondPic
  \caption{Second subfigure}
  \end{subfigure}
\caption{MAIN caption for SUBFIGURES}
\end{figure}


\begin{figure}
  \centering
        \subcaptionbox{First subcaptionbox}[0.45\textwidth]{\centering \firstPic}
          \hfill
        \subcaptionbox{Second subcaptionbox}[0.45\textwidth]{\centering \secondPic}
\caption{MAIN caption for SUBCAPTIONBOX}
\end{figure}

\end{document}

感谢 Tor 在聊天中给予我的帮助这里

相关内容