子图中的 Pgf-pie

子图中的 Pgf-pie

我有以下使用绘制四个饼图的代码subfigure

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{subfigure}
\usepackage{pgfplots}
\usepackage{pgf-pie}
\usepackage{xcolor}%
\definecolor{Azul}{rgb}{0.16, 0.32, 0.75}

\begin{document}

\begin{figure}[!htb]
\centering
\subfigure[Solution 1]{
\scalebox{0.8}{
\begin{tikzpicture}[scale=0.5]
\pie[radius=3,text=legend,
color={Azul!70,orange!70,red!70,yellow!70}]
{49.9/Ideal month,
18.2/One month,
9/Two months,
22.9/Three months}
\end{tikzpicture}}
}
\subfigure[Solution 2]{
\scalebox{0.8}{
\begin{tikzpicture}[scale=0.5]
\pie[radius=3,text=legend,
color={Azul!70,orange!70,red!70,yellow!70}]
{16.2/Ideal month,
31/One month,
20/Two months,
32.8/Three months}
\end{tikzpicture}}
}
\subfigure[Solution 3]{
\scalebox{0.8}{
\begin{tikzpicture}[scale=0.5]
\pie[radius=3,text=legend,
color={Azul!70,orange!70,red!70,yellow!70}]
{12.2/Ideal month,
18.7/One month,
20.6/Two months,
48.5/Three months}
\end{tikzpicture}}
}
\subfigure[Solution 4]{
\scalebox{0.8}{
\begin{tikzpicture}[scale=0.5]
\pie[radius=3,text=legend,
color={Azul!70,orange!70,red!70,yellow!70}]
{29.9/Ideal month,
17.2/One month,
19.3/Two months,
33.6/Three months}
\end{tikzpicture}}
}
\caption{Proportions of the total area harvested with different deviations (in module) from the ideal period}
\label{Proportion_time}
\end{figure}
\end{document}

其结果如下:

在此处输入图片描述

有没有办法只为四个图形绘制一个图例,避免冗余?但是,我想保持环境subfigure以区分一个图表与其他图表(因为标题不同...)。非常感谢。

答案1

您可以自行创建图例。以下是将图例放置在饼图顶部的示例。

\documentclass{article}
\usepackage{subfigure}
\usepackage{pgf-pie}

\definecolor{Azul}{rgb}{0.16, 0.32, 0.75}

\begin{document}
\begin{figure}[htb]
\centering

% create legend
% based on the code of the pgf-pie package
% https://github.com/pgf-tikz/pgf-pie/blob/master/tex/latex/pgf-pie/tikzlibrarypie.code.tex#L481-L492
\begin{tikzpicture}
    \coordinate (legendpos) at (0,0);
    \begin{scope}[node distance=2.5cm]
        \foreach \labelName/\color in {Ideal month/Azul!70, One month/orange!70, Two months/red!70, Three months/yellow!70} {
            \node[draw, fill={\color}, right of={legendpos}, label={0:{\labelName}}] (legendpos) {};
        }
    \end{scope}
\end{tikzpicture}

% code to create the pie charts with a caption
% copy and paste from the question
\subfigure[Solution 1]{
\scalebox{0.8}{
\begin{tikzpicture}[scale=0.5]
\pie[radius=3,% <- removed text=legend
color={Azul!70,orange!70,red!70,yellow!70}]
{49.9/Ideal month,
18.2/One month,
9/Two months,
22.9/Three months}
\end{tikzpicture}}
}
\subfigure[Solution 2]{
\scalebox{0.8}{
\begin{tikzpicture}[scale=0.5]
\pie[radius=3,% <- removed text=legend
color={Azul!70,orange!70,red!70,yellow!70}]
{16.2/Ideal month,
31/One month,
20/Two months,
32.8/Three months}
\end{tikzpicture}}
}
\subfigure[Solution 3]{
\scalebox{0.8}{
\begin{tikzpicture}[scale=0.5]
\pie[radius=3,% <- removed text=legend
color={Azul!70,orange!70,red!70,yellow!70}]
{12.2/Ideal month,
18.7/One month,
20.6/Two months,
48.5/Three months}
\end{tikzpicture}}
}
\subfigure[Solution 4]{
\scalebox{0.8}{
\begin{tikzpicture}[scale=0.5]
\pie[radius=3,% <- removed text=legend
color={Azul!70,orange!70,red!70,yellow!70}]
{29.9/Ideal month,
17.2/One month,
19.3/Two months,
33.6/Three months}
\end{tikzpicture}}
}
\caption{Proportions of the total area harvested with different deviations (in module) from the ideal period}
\label{Proportion_time}
\end{figure}
\end{document}

另请查看此邮政,因为您排列饼图的代码不是最好的。

答案2

这显示了如何使用\subfloat而不是\subfigure。此外,删除文本比删除 稍微困难一些[text=legend]。最后,pgf-pie 以不规则的方式添加额外空间,如红色矩形所示(可以且应该删除)。我过去常常\pgfresetboundingbox删除这个额外的空间并 \path用新的边界框替换它。

我还从那位不知名的恩人那里偷来了传说。

\documentclass{article}
\usepackage{subcaption}
\usepackage{pgf-pie}

\definecolor{Azul}{rgb}{0.16, 0.32, 0.75}

\begin{document}
\begin{figure}[htb]
\centering
\tikzset{every node/.style={font=\footnotesize}}
% based on the code of the pgf-pie package
% https://github.com/pgf-tikz/pgf-pie/blob/master/tex/latex/pgf-pie/tikzlibrarypie.code.tex#L481-L492
\begin{tikzpicture}
    \coordinate (legendpos) at (0,0);
    \begin{scope}[node distance=2.5cm]
        \foreach \labelName/\color in {Ideal month/Azul!70, One month/orange!70, Two months/red!70, Three months/yellow!70} {
            \node[draw, fill={\color}, right of={legendpos}, label={0:{\labelName}}] (legendpos) {};
        }
    \end{scope}
\end{tikzpicture}

\subfloat[Solution 1]{%
\begin{tikzpicture}[scale=0.4]
\pie[radius=3,text=inside,
color={Azul!70,orange!70,red!70,yellow!70}]
{49.9/,
18.2/,
9/,
22.9/}
\draw[red] (current bounding box.south east) rectangle (current bounding box.north west);
\pgfresetboundingbox
\path (3,3) (-3,-3);
\end{tikzpicture}%
}\hfil
\subfloat[Solution 2]{%
\begin{tikzpicture}[scale=0.4]
\pie[radius=3,text=inside,
color={Azul!70,orange!70,red!70,yellow!70}]
{16.2/,
31/,
20/,
32.8/}
\draw[red] (current bounding box.south east) rectangle (current bounding box.north west);
\pgfresetboundingbox
\path (3,3) (-3,-3);
\end{tikzpicture}%
}\hfil
\subfloat[Solution 3]{%
\begin{tikzpicture}[scale=0.4]
\pie[radius=3,text=inside,
color={Azul!70,orange!70,red!70,yellow!70}]
{12.2/,
18.7/,
20.6/,
48.5/}
\draw[red] (current bounding box.south east) rectangle (current bounding box.north west);
\pgfresetboundingbox
\path (3,3) (-3,-3);
\end{tikzpicture}%
}\hfil
\subfloat[Solution 4]{%
\begin{tikzpicture}[scale=0.4]
\pie[radius=3,text=inside,
color={Azul!70,orange!70,red!70,yellow!70}]
{29.9/,
17.2/,
19.3/,
33.6/}
\draw[red] (current bounding box.south east) rectangle (current bounding box.north west);
\pgfresetboundingbox
\path (3,3) (-3,-3);

\end{tikzpicture}%
}
\caption{Proportions of the total area harvested with different deviations (in module) from the ideal period}
\label{Proportion_time}
\end{figure}
\end{document}

演示

相关内容