使用 \subfloat 绘制自定义轮图的子图

使用 \subfloat 绘制自定义轮图的子图

我有三个图形,我想将它们组合成一个带有子标题的图形,因为它们具有相同的来源: 在此处输入图片描述

我正在使用 \subfloat 来制作我的其他数字(来自https://tex.stackexchange.com/a/246285/164280),这样它看起来就像这样: 在此处输入图片描述

每当我尝试使用 \subfloat 来制作我的自定义轮盘图时(来自https://tex.stackexchange.com/a/436536/164280https://tex.stackexchange.com/a/18105/164280),TeXstudio 说我缺少括号或括号太多。无论哪种情况它都会崩溃。我当前输出的代码是:

\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm,  top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{emptypage}
\usepackage[toc,page]{appendix}
\usepackage{tikz}
\usepackage[pdfpagelayout=TwoPageRight]{hyperref}
\usepackage{multicol}
\usepackage{apacite}
\usepackage{textcomp}
\usepackage{chngcntr}
\usepackage{adjustbox}
\hypersetup{colorlinks=true, linktoc=all, allcolors=green!30!black,}
\usepackage{booktabs, siunitx, caption}
\newcommand{\source}[1]{\vspace{-8pt} \caption*{ Source: {#1}} }
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{slashbox}
\usepackage[caption=false]{subfig}
\usepackage{array}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11,
/pgfplots/ybar legend/.style={
    /pgfplots/legend image code/.code={%
        \draw[##1,/tikz/.cd,yshift=-0.25em]
        (0cm,0cm) rectangle (3pt,0.8em);},},}
\usetikzlibrary{arrows}
\begin{filecontents}{installations.csv}
Name,   Quantity
"Japan", 66
"China", 12
"Korea", 8
"Other", 23
\end{filecontents}

\begin{filecontents}{installedcapacity.csv}
Name,   Quantity
"Japan", 95.516
"China", 394.258
"Korea", 16.901
"Other", 14.589
\end{filecontents}

\begin{document}
%Wheelchart specifications
\pgfplotstableread[ignore chars=",col sep=comma]{installations.csv}\datainstallations
\pgfplotstableread[ignore chars=",col sep=comma]{installedcapacity.csv}\datacapacity
\pgfplotstableread{
clr
blue!70!black
red!70!black
black!70!white
white!70!black
}\MyColors

% Adjusts the size of the wheel:
\def\innerradius{1.5cm}
\def\outerradius{2cm}

\newcommand\LabelName{}
\newcommand\LabelValue{}

% The main macro
\newcommand{\wheelchart}[2]{ % <--- (from 1 to 2)
% Calculate total
\pgfmathsetmacro{\totalnum}{0}
% get number of rows in table
\pgfplotstablegetrowsof{#1}
% minus 1 because indexing starts as zero
\pgfmathsetmacro{\RowsInTable}{\pgfplotsretval-1}
\foreach \i in {0,...,\RowsInTable} {
    \pgfplotstablegetelem{\i}{Quantity}\of{#1}
    \pgfmathparse{\pgfplotsretval+\totalnum}
    \global\let\totalnum=\pgfmathresult
}
\begin{center}
    \begin{tikzpicture}
    % The text in the center of the wheel
    \node[align=center,text width=2*\innerradius]            {\pgfmathprintnumber{\totalnum}~{#2}}; % <--- (name is now defined by second     option of the command \wheelchart
    % Calculate the thickness and the middle line of the wheel
    \pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
    \pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}

    % Rotate so we start from the top
    \begin{scope}[rotate=90]
    % Loop through each value set. \cumnum keeps track of where we are in the wheel
    \pgfmathsetmacro{\cumnum}{0}
    \foreach \i in {0,...,\RowsInTable} {
        % get values from table
        \pgfplotstablegetelem{\i}{Name}\of{#1}\global\let\LabelName=\pgfplotsretval
        \pgfplotstablegetelem{\i}{Quantity}\of{#1}\renewcommand\LabelValue{\pgfplotsretval}

        \pgfmathsetmacro{\newcumnum}{\cumnum + \LabelValue/\totalnum*360}

        % Calculate the percent value
        \pgfmathsetmacro{\percentage}{\LabelValue}
        % Calculate the mid angle of the colour segments to place the labels
        \pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}

        % This is necessary for the labels to align nicely
        \pgfmathparse{
            (-\midangle<180?"west":"east")
        } \edef\textanchor{\pgfmathresult}
        \pgfmathsetmacro\labelshiftdir{ifthenelse(\RowsInTable==0,-1,1)*(1-2*(-\midangle>180))}

        % Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
        \pgfplotstablegetelem{\i}{clr}\of{\MyColors}
        \fill[color=\pgfplotsretval] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
        (-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;

        % Draw the data labels
        \pgfmathsetmacro\labelmag{
            1 + .9*(abs(1/sin(\midangle)))^1 - .03*(abs(1/sin(\midangle)))^2}
        \draw  [*-,thin] node [append after command={(\midangle:\midradius pt) --
            (\midangle:\outerradius + \labelmag ex) -- (\tikzlastnode)}] at
        (\midangle:\outerradius + \labelmag ex)
        [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\LabelName: \pgfmathprintnumber{\percentage}};

        % Set the old cumulated angle to the new value
        \global\let\cumnum=\newcumnum
    }
    \end{scope}
    %      \draw[gray] (0,0) circle (\outerradius) circle (\innerradius);
    \end{tikzpicture}
\end{center}
}

\begin{figure}[!htb]
\centering
\begin{tikzpicture}
\begin{axis}[
width=0.9\linewidth,
height=0.4\linewidth,
ybar,bar width=25pt, enlarge x limits=0.15, ymin=0, 
legend style={at={(0.5,1.15)},anchor=north,legend columns=2},
ylabel={Installed capacity (MW\textsubscript{p})},
xticklabels={$<2014$, $2014$, $2015$, $2016$, $2017$}, % set ticklabels explicitly
xtick=data, nodes near coords,  axis lines*=left, ymajorgrids
]
\addplot[red!50!black, fill=red!70!black] coordinates {(2013, 1.7) (2014, 3.0) (2015, 49.5) (2016, 70.7) (2017, 396.5)};
\addplot[blue!50!black, fill=blue!70!black] coordinates {(2013, 1.7) (2014, 4.7) (2015, 54.2) (2016, 124.9)(2017, 521.4)};
\legend{Annual installed capacity, Cumulative installed capacity}
\end{axis}
\end{tikzpicture}
\caption{Global installed capacity of floating photovoltaics}
\label{fig:capacity over time}
\end{figure}

我想要做的是:

\begin{figure}[h]
\centering
\subfloat[Global installed capacity of floating photovoltaics]{%
\begin{tikzpicture}
\begin{axis}[
width=0.9\linewidth,
height=0.4\linewidth,
ybar,bar width=25pt, enlarge x limits=0.15, ymin=0, 
legend style={at={(0.5,1.15)},anchor=north,legend columns=2},
ylabel={Installed capacity (MW\textsubscript{p})},
xticklabels={$<2014$, $2014$, $2015$, $2016$, $2017$}, % set ticklabels explicitly
xtick=data, nodes near coords,  axis lines*=left, ymajorgrids
]
\addplot[red!50!black, fill=red!70!black] coordinates {(2013, 1.7) (2014, 3.0) (2015, 49.5) (2016, 70.7) (2017, 396.5)};
\addplot[blue!50!black, fill=blue!70!black] coordinates {(2013, 1.7) (2014, 4.7) (2015, 54.2) (2016, 124.9)(2017, 521.4)};
\legend{Annual installed capacity, Cumulative installed capacity}
\end{axis}
\end{tikzpicture}
}
\subfloat[Installations per country]{%
\wheelchart{\datainstallations}{Installations}
}
\subfloat[Installed capacity per country]{%
\wheelchart{\datacapacity}{MW\textsubscript{p}}
}   
\caption{FPV installations and capacity in late 2017}
\label{fig:capacity and installations per country}
\source{Adapted from \shortcite{DeJong2018}}    
\end{figure}

希望你们明白我的意思。我想将图 1.1、1.2 和 1.3 改为 1.1 (a)、1.1 (b)、1.1 (c),并在底部添加来源

答案1

似乎不能center在 内拥有环境\subfloat,并且 的定义\wheelchart包含一个。注释掉/删除宏中的\begin{center}和。\end{center}\wheelchart

我还对 做了一些其他更改,请参阅代码中的注释。最值得注意的是,我在行尾\wheelchart添加了一堆,以避免出现虚假空格。我还添加了一个传递给 的可选参数,例如,它可以让您缩放图表。%tikzpicture

编辑:可能有几种方法可以修复垂直对齐。一种方法是将类似的东西添加\path (-90:1.25*\outerradius);tikzpicture。这将向下延伸边界框,如果两个车轮的边界框在车轮下方延伸相同的距离,则对齐是正确的。为了避免将其设为默认值(即使在不需要的情况下也会导致额外的空白),您可以使用以下方式定义新样式

\tikzset{
  raisewheel/.style={
    execute at end picture={
      \path (-90:#1*\outerradius);
    }
  },
  raisewheel/.default=1.3
}

并使用例如

\wheelchart[scale=0.8,font=\footnotesize,raisewheel]{\datainstallations}{Installations}

也许不是很优雅,但它确实有效。

在此处输入图片描述

\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm,  top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{emptypage}
\usepackage[toc,page]{appendix}
\usepackage{tikz}
\usepackage[pdfpagelayout=TwoPageRight]{hyperref}
\usepackage{multicol}
\usepackage{apacite}
\usepackage{textcomp}
\usepackage{chngcntr}
\usepackage{adjustbox}
\hypersetup{colorlinks=true, linktoc=all, allcolors=green!30!black,}
\usepackage{booktabs, siunitx, caption}
\newcommand{\source}[1]{\vspace{-8pt} \caption*{ Source: {#1}} }
\usepackage{amsmath}
\usepackage{graphicx}
%\usepackage{slashbox}
\usepackage[caption=false]{subfig}
\usepackage{array}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11,
/pgfplots/ybar legend/.style={
    /pgfplots/legend image code/.code={%
        \draw[##1,/tikz/.cd,yshift=-0.25em]
        (0cm,0cm) rectangle (3pt,0.8em);},},}
\usetikzlibrary{arrows,backgrounds}


\tikzset{
  raisewheel/.style={
    execute at end picture={
      \path (-90:#1*\outerradius);
    }
  },
  raisewheel/.default=1.3
}

\begin{filecontents}{installations.csv}
Name,   Quantity
"Japan", 66
"China", 12
"Korea", 8
"Other", 23
\end{filecontents}

\begin{filecontents}{installedcapacity.csv}
Name,   Quantity
"Japan", 95.516
"China", 394.258
"Korea", 16.901
"Other", 14.589
\end{filecontents}

\begin{document}
%Wheelchart specifications
\pgfplotstableread[ignore chars=",col sep=comma]{installations.csv}\datainstallations
\pgfplotstableread[ignore chars=",col sep=comma]{installedcapacity.csv}\datacapacity
\pgfplotstableread{
clr
blue!70!black
red!70!black
black!70!white
white!70!black
}\MyColors

% Adjusts the size of the wheel:
\def\innerradius{1.6cm}
\def\outerradius{2cm}

\newcommand\LabelName{}
\newcommand\LabelValue{}

% The main macro
\newcommand{\wheelchart}[3][]{ % <--- (from 2 to 3, first argument optional)
% Calculate total
\pgfmathsetmacro{\totalnum}{0}% <-- added %
% get number of rows in table
\pgfplotstablegetrowsof{#2}% <-- added %
% minus 1 because indexing starts as zero
\pgfmathsetmacro{\RowsInTable}{\pgfplotsretval-1}% <-- added %
\foreach \i in {0,...,\RowsInTable} {% <-- added %
    \pgfplotstablegetelem{\i}{Quantity}\of{#2}% <-- added %
    \pgfmathparse{\pgfplotsretval+\totalnum}% <-- added %
    \global\let\totalnum=\pgfmathresult% <-- added %
}
%\begin{center}  % <---- commented
    \begin{tikzpicture}[#1] % <--- added [#1]
    % The text in the center of the wheel
    \node[align=center,text width=2*\innerradius]            {\pgfmathprintnumber{\totalnum}~{#3}}; % <--- (name is now defined by second     option of the command \wheelchart
    % Calculate the thickness and the middle line of the wheel
    \pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
    \pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}

    % Rotate so we start from the top
    \begin{scope}[rotate=90]
    % Loop through each value set. \cumnum keeps track of where we are in the wheel
    \pgfmathsetmacro{\cumnum}{0}
    \foreach \i in {0,...,\RowsInTable} {
        % get values from table
        \pgfplotstablegetelem{\i}{Name}\of{#2}\global\let\LabelName=\pgfplotsretval
        \pgfplotstablegetelem{\i}{Quantity}\of{#2}\renewcommand\LabelValue{\pgfplotsretval}

        \pgfmathsetmacro{\newcumnum}{\cumnum + \LabelValue/\totalnum*360}

        % Calculate the percent value
        \pgfmathsetmacro{\percentage}{\LabelValue}
        % Calculate the mid angle of the colour segments to place the labels
        \pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}

        % This is necessary for the labels to align nicely
        \pgfmathparse{
            (-\midangle<180?"west":"east")
        } \edef\textanchor{\pgfmathresult}
        \pgfmathsetmacro\labelshiftdir{ifthenelse(\RowsInTable==0,-1,1)*(1-2*(-\midangle>180))}

        % Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
        \pgfplotstablegetelem{\i}{clr}\of{\MyColors}
        \fill[color=\pgfplotsretval] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
        (-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;

        % Draw the data labels
        \pgfmathsetmacro\labelmag{
            1 + .9*(abs(1/sin(\midangle)))^1 - .03*(abs(1/sin(\midangle)))^2}
        \draw  [*-,thin] node [append after command={(\midangle:\midradius pt) --
            (\midangle:\outerradius + \labelmag ex) -- (\tikzlastnode)}] at
        (\midangle:\outerradius + \labelmag ex)
        [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\LabelName: \pgfmathprintnumber{\percentage}};

        % Set the old cumulated angle to the new value
        \global\let\cumnum=\newcumnum
    }
    \end{scope}
    %      \draw[gray] (0,0) circle (\outerradius) circle (\innerradius);
    \end{tikzpicture}
%\end{center} % <---- commented
}


\begin{figure}[h]
\centering
\subfloat[Global installed capacity of floating photovoltaics]{%
\begin{tikzpicture}
\begin{axis}[
width=0.9\linewidth,
height=0.4\linewidth,
ybar,bar width=25pt, enlarge x limits=0.15, ymin=0, 
legend style={at={(0.5,1.15)},anchor=north,legend columns=2},
ylabel={Installed capacity (MW\textsubscript{p})},
xticklabels={$<2014$, $2014$, $2015$, $2016$, $2017$}, % set ticklabels explicitly
xtick=data, nodes near coords,  axis lines*=left, ymajorgrids
]
\addplot[red!50!black, fill=red!70!black] coordinates {(2013, 1.7) (2014, 3.0) (2015, 49.5) (2016, 70.7) (2017, 396.5)};
\addplot[blue!50!black, fill=blue!70!black] coordinates {(2013, 1.7) (2014, 4.7) (2015, 54.2) (2016, 124.9)(2017, 521.4)};
\legend{Annual installed capacity, Cumulative installed capacity}
\end{axis}
\end{tikzpicture}
}

\subfloat[Installations per country]{%
\wheelchart[scale=0.8,font=\footnotesize,raisewheel]{\datainstallations}{Installations}
}%
\subfloat[Installed capacity per country]{%
    \wheelchart[scale=0.8,font=\footnotesize,raisewheel]{\datacapacity}{MW\textsubscript{p}}
}
\caption{FPV installations and capacity in late 2017}
\label{fig:capacity and installations per country}
\source{Adapted from \shortcite{DeJong2018}}    
\end{figure}

\end{document}

相关内容