TransWikia.com

Reduce horizontal gap between legend and pie chart

TeX - LaTeX Asked by Yogesh Sharma on June 25, 2021

How to reduce the gab between the pie chart and legend. Following is my code

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}

One Answer

While it is much work to go through, the technique I found to work is to superimpose pies, with each superimposition providing a needed part of the final result. Here, I superimpose 3 pies in the revised figure. The first adjusts the legend by plotting a smaller pie (which is later covered up). The second fixes another problem with the OP's plot, which is that small slice percentages overlap and bleed past the side of their slice, so I move the label to the outside. The third pie replots those slices that can support having their percent on top of the slice itself.

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}

enter image description here

Correct answer by Steven B. Segletes on June 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP