如何减少饼图和图例之间的差距。以下是我的代码
\documentclass{article}
\usepackage{pgf-pie}
\usepackage{tikz,pgfplots,pgfplotstable}
\begin{document}
\begin{figure*}[h]
\centering
\begin{tikzpicture}
[
every node/.style={align=center},
pin distance=9mm,
font={\small},
scale=0.65
]
\pie[
/tikz/every pin/.style={align=center},
every only number node/.style={text=white},
text=legend,
rotate=240,
explode=0.2,
] {33/{2020}, 24/{2019}, 27/{2018}, 12/{2017}, 2/{2016}, 2/{2015}}
\end{tikzpicture}
\end{figure*}
\end{document}
答案1
虽然需要做很多工作,但我发现可行的方法是叠加饼图,每次叠加都会提供最终结果所需的部分。在这里,我在修改后的图中叠加了 3 个饼图。第一个饼图通过绘制一个较小的饼图(稍后会覆盖)来调整图例。第二个饼图解决了 OP 图的另一个问题,即小切片百分比重叠并超出其切片的侧面,因此我将标签移到外面。第三个饼图重新绘制了那些可以支持将其百分比放在切片本身顶部的切片。
\documentclass{article}
\usepackage{pgf-pie}
\usepackage{tikz,pgfplots,pgfplotstable}
\begin{document}
\begin{figure*}[h]
\centering
\begin{tikzpicture}
[
every node/.style={align=center},
pin distance=9mm,
font={\small},
scale=0.65
]
\pie[
/tikz/every pin/.style={align=center},
every only number node/.style={text=white},
text=legend,
rotate=240,
explode=0.2,
] {33/{2020}, 24/{2019}, 27/{2018}, 12/{2017}, 2/{2016}, 2/{2015}}
\end{tikzpicture}
\caption{Original}
\end{figure*}
\begin{figure*}[h]
\centering
\begin{tikzpicture}
[
every node/.style={align=center},
pin distance=9mm,
font={\small},
scale=0.65
]
\pie[
/tikz/every pin/.style={align=center},
every only number node/.style={text=white},
text=legend,
radius=2.3, % PIE SIZE WILL SHIFT THE LEGEND,
hide number, % DON'T WANT 2% NUMBERS VISIBLE ON PIE
rotate=240,
explode=0.2,
] {33/{2020}, 24/{2019}, 27/{2018}, 12/{2017}, 2/{2016}, 2/{2015}}
%
\pie[
/tikz/every pin/.style={align=center},
every only number node/.style={text=white},
rotate=225.6,% PUT 2% SLICES WHERE THEY BELONG, explode REQUIRES TWEAK
hide number,% DON'T ACTUALLY PRINT NUMBERS ON PIE
explode=0.2,
] {0/,0/,0/,0/, % ZEROS REQUIRED TO GET SLICE COLORS RIGHT
2/2\%, 2/\raisebox{-10pt}{\rlap{2\%}~~}% PLOT % AS LABEL, ADJUST
}
%
\pie[
/tikz/every pin/.style={align=center},
every only number node/.style={text=white},
rotate=240,
explode=0.2,
] {33/, 24/, 27/, 12/} % ONLY PLOT SLICES WHERE I WANT NUMBERS
\end{tikzpicture}
\caption{Revised}
\end{figure*}
\end{document}
答案2
这轮图我写的包,可以使用。
定义一个命令\WCtest
,其输出取决于百分比是否大于 10。此命令用于键data
和wheel data
。此处,使用 来获取百分比\WCperc
。
使用 键移动切片explode
。
图例由键决定legend
。这是一个tabular
放置在 中的\node
。行由键决定legend row
。可以使用 获得所有行的结果\WClegend
。
该宏\WClistcolors
引用了给予键的列表中的项目WClistcolors
。
\documentclass[border=6pt]{standalone}
\usepackage{wheelchart}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\def\WCtest#1#2{%
\pgfmathparse{\WCpercentage>10?"#1":"#2"}%
\pgfmathresult%
}
\wheelchart[
counterclockwise,
data=\WCtest{}{\WCperc},
explode,
legend row={\tikz\fill[\WClistcolors,draw=black] (0,0) rectangle (0.3,0.3); & \WCvarB},
legend={
\node[anchor=west] at (3.5,0) {%
\begin{tabular}{l@{ }l}%
\WClegend%
\end{tabular}%
};
},
pie,
slices style={\WClistcolors,draw=black},
start angle=240,
WClistcolors={blue!60,cyan!60,yellow!60,orange!60,red!60,blue!60!cyan!60,cyan!60!yellow!60,red!60!cyan!60,red!60!blue!60,orange!60!cyan!60},
wheel data=\WCtest{\WCperc}{}
]{%
33/2020,
24/2019,
27/2018,
12/2017,
2/2016,
2/2015%
}
\end{tikzpicture}
\end{document}