带合并图例位置的饼图

带合并图例位置的饼图

我是 LaTeX 新手,希望有人能帮助我。我被这个数字困扰了好久,却找不到解决办法。

具体来说,我制作了 2 个并排的饼图,并使用了相连的图例。默认选项似乎是将图例框堆叠在图表之间。我该怎么做:

  1. 将图例向下移动到框的南下方(我尝试过 \legend.style,但似乎没有任何作用)
  2. 在每个框的顶部添加一个文本以指定数据来自哪个组(即,左侧框标记为组 1,右侧框标记为组 2)。

非常感谢您的帮助!

我的 MWE 是:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgf-pie}

\begin{document}\begin{figure}[h!]
\begin{tikzpicture}
\tikzset{lines/.style={draw=white},}


\pie [square, scale font,
            color = {blue!10, blue!20, blue!40}, sum=auto, text=legend, after number = {\%}, every only number node/.style={text=black},style={lines}]{58/Unidentifiable ML,25/MOP only,11/SMO only,6/MOP \& SMP} 

\pie [pos={11,0}, square, scale font,
            color = {blue!10, blue!20, blue!40}, sum=auto, after number = {\%},every only number node/.style={text=black},style={lines}]{56/,18/,10/,16/}


\end{tikzpicture}
\caption{How do I move the legend down south}
\label{fg:R1pie}
\end{figure}

\end{document}

答案1

我认为条形图或圆形图更方便,但如果你坚持使用那种矩形,那么下面是使用纯 TikZ 的一种方法。纯 TikZ 使控制更容易。我喜欢这幅图的另一个原因是它说明了 TikZ 绘图的方式:将nodepic沿着放置path

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.8]  
\tikzset{box/.pic={
\fill[#1] (-.2,-.2) rectangle (.2,.2);
}}

\begin{scope}[yshift=2mm]
\path
(0,0)     pic[fill=blue!10]{box} node[right=2mm]{MOP \& SMP}
++(90:.6) pic[fill=blue!20]{box} node[right=2mm]{SMO only}
++(90:.6) pic[fill=blue!30]{box} node[right=2mm]{MOP only}
++(90:.6) pic[fill=blue!40]{box} node[right=2mm]{Unidentifiable ML};
\end{scope}

\newcommand{\block}[6]{
\fill[#1,draw=#2] #3 rectangle #4;
\path ($.5*#4+.5*#3$) node[scale=#5]{#6};
}

\begin{scope}[shift={(180:7)},scale=6,local bounding box=L]
\block{blue!10}{white}{(0,0)}{(.58,1)}{2.5}{$58\%$}
\block{blue!20}{white}{(.58,0)}{(1,25/42)}{2}{$25\%$}
\block{blue!30}{white}{(.58,25/42)}{(1,36/42)}{1.5}{$11\%$}
\block{blue!40}{white}{(.58,36/42)}{(1,1)}{1.2}{$6\%$}
\path (L.north) node[above,scale=2]{\bfseries Group 1};
\end{scope}

\begin{scope}[shift={(0:4.5)},scale=6,local bounding box=R]
\block{blue!10}{white}{(0,0)}{(.56,1)}{2.5}{$56\%$}
\block{blue!20}{white}{(.56,0)}{(1,18/44)}{2}{$18\%$}
\block{blue!30}{white}{(.56,18/44)}{(1,28/44)}{1.5}{$10\%$}
\block{blue!40}{white}{(.56,28/44)}{(1,1)}{2}{$16\%$}
\path (R.north) node[above,scale=2]{\bfseries Group 2};
\end{scope}
\end{tikzpicture}
\end{center}
\end{document} 

答案2

也许可以通过改变原始代码的参数pgf-pie.sty

% legend
  \iflegend
  \coordinate[xshift=0.8cm,
  yshift=(\value{pgfpie@sliceLength}*0.5+1)*0.5cm] (legendpos) at
  (current bounding box.east);

作为“代码风格”:

\tikzset{
legend to the south/.code={
\coordinate[xshift=-1.5cm,
yshift=(\value{pgfpie@sliceLength}*0.5+1)*0.5cm-4em] (legendpos) at
(current bounding box.south);},
}

进而:\pie[style={legend to the south}, ...]

在此处输入图片描述

\documentclass[border=5pt, tikz]{standalone}
\usepackage{pgf-pie}

\begin{document}
\tikzset{
legend to the south/.code={
\coordinate[xshift=-1.5cm,
yshift=(\value{pgfpie@sliceLength}*0.5+1)*0.5cm-4em] (legendpos) at
(current bounding box.south);},
}

\begin{tikzpicture}[
every path/.style={draw=white, very thick}
]
\pie[square, 
scale font,
color = {blue!10, blue!20, blue!40}, 
sum=auto, text=legend, 
after number = {\%}, 
style={legend to the south}
]{58/Unidentifiable ML,25/MOP only,11/SMO only,6/MOP \& SMP} 
\end {tikzpicture}
\end{document}

相关内容