Tikz - 包含许多数据的饼图

Tikz - 包含许多数据的饼图

我正在尝试使用 以饼图形式显示数据Tikz。我使用了饼图示例texample.net

我的问题是我想要呈现大量的数据,因此有很多切片,结果饼图看起来很丑......

关于如何使其更加干净和清晰,有什么想法吗?

下面是重现该图的代码

\documentclass{article}

\usepackage{tikz}
\usepackage{calc}
\usepackage{ifthen}

\begin{document}

\newcommand{\slice}[4]{
  \pgfmathparse{0.5*#1+0.5*#2}
  \let\midangle\pgfmathresult

  % slice
  \draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

  % outer label
  \node[label=\midangle:#4] at (\midangle:1) {};

  % inner label
  \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
  \let\temp\pgfmathresult
  \pgfmathparse{max(\temp,-0.5) + 0.8}
  \let\innerpos\pgfmathresult
  \node at (\midangle:\innerpos) {#3};
}

\begin{tikzpicture}[scale=5]

\newcounter{a}
\newcounter{b}
\foreach \p/\t in {3/Argentina,
                   1/Armenia,
                   7/Belgium,
                   2/Brazil,
                   2/Bulgaria,
                   19/Canada,
                   37/China,
                   6/China-Taiwan,
                   6/Czech Republic,
                   4/Finland,
                   58/France,
                   8/Germany,
                   4/Hungary,
                   22/India,
                   1/Iran,
                   42/Japan,
                   2/Mexico,
                   1/Netherlands,
                   4/Pakistan,
                   25/Republic of Korea,
                   2/Romania,
                   35/Russia,
                   4/Slovakia,
                   1/Slovenia,
                   2/South Africa,
                   7/Spain,
                   10/Sweeden,
                   5/Switzerland,
                   15/Ukraine,
                   15/United Kingdom,
                   99/United States of America
                   }
  {
    \setcounter{a}{\value{b}}
    \addtocounter{b}{\p}
    \slice{\thea/449*360}
          {\theb/449*360}
          {\p}{\t}
  }

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

制作一个非常基本的条形图相当容易,至于它是否繁忙,我留给你去判断。明显的好处是你不会遇到标签重叠的问题。双列选项适用于希望节省垂直空间的情况,尽管它确实会降低一些可比性。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\tikzset{
  barlabels/.style={font=\footnotesize\sffamily},
  declare function={
    barheight=5pt;
  }
}
\begin{document}
\begin{center}
\begin{tikzpicture}[
  y=0.3cm,
  x=0.06cm,
]

\foreach [count=\i from 0] \p/\t in
                  {3/Argentina,
                   1/Armenia,
                   7/Belgium,
                   2/Brazil,
                   2/Bulgaria,
                   19/Canada,
                   37/China,
                   6/China-Taiwan,
                   6/Czech Republic,
                   4/Finland,
                   58/France,
                   8/Germany,
                   4/Hungary,
                   22/India,
                   1/Iran,
                   42/Japan,
                   2/Mexico,
                   1/Netherlands,
                   4/Pakistan,
                   25/Republic of Korea,
                   2/Romania,
                   35/Russia,
                   4/Slovakia,
                   1/Slovenia,
                   2/South Africa,
                   7/Spain,
                   10/Sweden,
                   5/Switzerland,
                   15/Ukraine,
                   15/United Kingdom,
                   99/United States of America
                   }
  {
   \node [anchor=base east,
          barlabels,
          name=i-\i] at (0,-\i) {\t};
   \fill [blue!40] (i-\i.base east) rectangle ++(\p,barheight)  ++(0,-barheight)
          node[barlabels, 
               black,
               anchor=base west] {\p};
  }

\end{tikzpicture}

\bigskip

Two columns:

\bigskip

\begin{tikzpicture}[
  y=0.3cm,
  x=0.04cm,
  barlabels/.style={font=\footnotesize\sffamily}
]

\foreach [count=\i from 0] \p/\t in
                  {3/Argentina,
                   1/Armenia,
                   7/Belgium,
                   2/Brazil,
                   2/Bulgaria,
                   19/Canada,
                   37/China,
                   6/China-Taiwan,
                   6/Czech Republic,
                   4/Finland,
                   58/France,
                   8/Germany,
                   4/Hungary,
                   22/India,
                   1/Iran,
                   42/Japan,
                   2/Mexico,
                   1/Netherlands,
                   4/Pakistan,
                   25/Republic of Korea,
                   2/Romania,
                   35/Russia,
                   4/Slovakia,
                   1/Slovenia,
                   2/South Africa,
                   7/Spain,
                   10/Sweden,
                   5/Switzerland,
                   15/Ukraine,
                   15/United Kingdom,
                   99/United States of America
                   }
  {
   \node [anchor=base east,
          barlabels,
          name=i-\i] at ({ifthenelse(\i < 16,0,150)},{-mod(\i,16)}) {\t};
   \fill [blue!40] (i-\i.base east) rectangle ++(\p,barheight)  ++(0,-barheight)
          node[barlabels, 
               black,
               anchor=base west] {\p};
  }

\end{tikzpicture}
\end{center}
\end{document}

我想,如果您必须有一个包含如此多小切片的饼图,那么您需要某种避免重叠的方法,例如通过移动单个标签。对代码进行这个非常简单的修改,为宏添加了两个参数\slice和两个相应的循环变量。第一个用于将文本标签移动(x,y),第二个用于径向移动数字(它是半径乘以的因子)。我还将切片本身移动到背景层,这样它们就不会覆盖数字。

我并不是说这个图很漂亮,而且可能有更好的方法,但它展示了一种让图表更易读的方法。在某种程度上。

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}

\newcommand{\slice}[6]{
  \pgfmathparse{0.5*#1+0.5*#2}
  \let\midangle\pgfmathresult

  % slice
  \scoped[on background layer]
     \draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

  % outer label
  \node[label={[shift={#5}]\midangle:#4}] at (\midangle:1) {};

  % inner label
  \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
  \let\temp\pgfmathresult
  \pgfmathparse{max(\temp,-0.5) + 0.8}
  \let\innerpos\pgfmathresult
  \node [inner sep=1pt,circle,fill=black!10] at (\midangle:\innerpos*#6) {#3};
}

\begin{tikzpicture}[scale=5]

\newcounter{a}
\newcounter{b}
\foreach \p/\t/\nodeshift/\numbershift in 
                  {3/Argentina/{(0,-0.1)}/1.1,
                   1/Armenia/{(0,0.1)}/1,
                   7/Belgium/{(0,-0.1)}/0.9,
                   2/Brazil/{(0,0)}/1.1,
                   2/Bulgaria/{(0,0.1)}/1,
                   19/Canada/{(0,0)}/1,
                   37/China/{(0,0)}/1,
                   6/China-Taiwan/{(0,0)}/1,
                   6/Czech Republic/{(0,0.4)}/1,
                   4/Finland/{(-0.5,0)}/1,
                   58/France/{(0,0)}/1,
                   8/Germany/{(0,0)}/1,
                   4/Hungary/{(0,0)}/1,
                   22/India/{(0,0)}/1,
                   1/Iran/{(0,0)}/1,
                   42/Japan/{(0,0)}/1,
                   2/Mexico/{(0,0.2)}/0.9,
                   1/Netherlands/{(0,0)}/1,
                   4/Pakistan/{(0,-0.2)}/1.1,
                   25/Republic of Korea/{(0,0)}/1,
                   2/Romania/{(0,0)}/1,
                   35/Russia/{(0,0)}/1,
                   4/Slovakia/{(0,0.4)}/0.7,
                   1/Slovenia/{(0,0.2)}/0.8,
                   2/South Africa/{(0,0)}/0.9,
                   7/Spain/{(0,-0.1)}/1,
                   10/Sweden/{(0,-0.15)}/1,
                   5/Switzerland/{(0,-0.3)}/1,
                   15/Ukraine/{(0.4,0.2)}/1,
                   15/United Kingdom/{(0,0)}/1,
                   99/United States of America/{(0,0)}/1}
  {
    \setcounter{a}{\value{b}}
    \addtocounter{b}{\p}
    \slice{\thea/449*360}
          {\theb/449*360}
          {\p}{\t}{\nodeshift}{\numbershift}
  }

\end{tikzpicture}

\end{document}

相关内容