我总是无法让图表正确居中。它们似乎总是错开到页面的右侧。请注意示例图像如何完美居中,就好像它们是单个图形一样,但图表却不是。它们的标题似乎正确居中,但图表相对于标题而言是不平衡的。我尝试使用 minipages 和 makeboxes 等来修复它,但我无法弄清楚。解决方案是什么?干杯。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass{article}
\usepackage{xcolor,tikz}
\usetikzlibrary{backgrounds}
\definecolor{NR}{HTML}{D43F3A}
\definecolor{NO}{HTML}{EE9336}
\definecolor{NY}{HTML}{FDC431}
\definecolor{NG}{HTML}{3FAE49}
\definecolor{NB}{HTML}{0071B9}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfkeys{/donut/.cd,
inner radius/.initial=1cm,
inner radius=1cm,
outer radius/.initial=3.14cm,
outer radius=3.14cm,
text color/.initial=white,
text color=white}
\newcommand{\donutchart}[2][]{% Calculate total
\pgfmathsetmacro{\totalnum}{0}
\foreach [count=\n] \value/\colour/\name in {#2} {
\pgfmathparse{\value+\totalnum}
\global\let\totalnum=\pgfmathresult
\xdef\numitems{\n}}
\begin{tikzpicture}
\pgfmathsetmacro{\wheelwidth}{\pgfkeysvalueof{/donut/outer
radius}-\pgfkeysvalueof{/donut/inner radius}}
\pgfmathsetmacro{\midradius}{(\pgfkeysvalueof{/donut/outer radius}
+\pgfkeysvalueof{/donut/inner radius})/2}
\begin{scope}[#1]
\pgfmathsetmacro{\cumnum}{0}
\foreach \value/\colour/\name in {#2} {
\pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}
\pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}
\begin{scope}[on background layer]
\filldraw[draw=white,fill=\colour]
(-\cumnum:\pgfkeysvalueof{/donut/outer radius})
arc(-\cumnum:-(\newcumnum):\pgfkeysvalueof{/donut/outer radius}) --
(-\newcumnum:\pgfkeysvalueof{/donut/inner radius})
arc(-\newcumnum:-(\cumnum):\pgfkeysvalueof{/donut/inner radius}) -- cycle;
\end{scope}
\draw node [text=\pgfkeysvalueof{/donut/text color},
font=\bfseries\sffamily] at
(\midangle:{\pgfkeysvalueof{/donut/inner radius}+\wheelwidth/2}) {\name};
\global\let\cumnum=\newcumnum}
\end{scope}
\end{tikzpicture}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}
\centering
\begin{minipage}{0.45\textwidth}
\centering
\donutchart[rotate=45,]{1/NR/, 1/NO/, 20/NY/, 5/NG/, 73/NB/}
\caption{Caption 1}
\end{minipage}\hfill
\begin{minipage}{0.45\textwidth}
\centering
\donutchart[rotate=45]{1/NR/, 1/NO/, 20/NY/, 5/NG/, 73/NB/}
\caption{Caption 2}
\end{minipage}\hfill
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}
\centering
\begin{minipage}{0.45\textwidth}
\centering
\includegraphics[width=1\textwidth]{example-image-a}
\caption{Caption 3}
\end{minipage}\hfill
\begin{minipage}{0.45\textwidth}
\centering
\includegraphics[width=1\textwidth]{example-image-b}
\caption{Caption 4}
\end{minipage}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
参考:
答案1
有三条评论,均属实:
- 虚假空间;
- 对边界框有贡献的空节点;
- 正如扎尔科 (Zarko) 所指出的,您的尺寸太大,不适合。
然而,根据我的发现,主要的影响是圆弧的边界框计算是错误的。圆弧越长,问题就越严重。长圆弧在左边,所以这会导致向右的大幅偏移。所以我通过手动剪切圆的正确边界框解决了这个问题。除此之外,
- 我通过向上移动来处理空间
\begin{tikzpicture}
。 - 确保仅当节点不为空时才会添加节点。
- 稍微减小了尺寸。
结果是
\documentclass{article}
\usepackage{xcolor,tikz}
\usetikzlibrary{backgrounds}
\definecolor{NR}{HTML}{D43F3A}
\definecolor{NO}{HTML}{EE9336}
\definecolor{NY}{HTML}{FDC431}
\definecolor{NG}{HTML}{3FAE49}
\definecolor{NB}{HTML}{0071B9}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfkeys{/donut/.cd,
inner radius/.initial=1cm,
inner radius=1cm,
outer radius/.initial=3.14cm,
outer radius=0.5\textwidth-0.5*\pgflinewidth,
text color/.initial=white,
text color=white}
\newcommand{\donutchart}[2][]{% Calculate total
\begin{tikzpicture}
\pgfmathsetmacro{\totalnum}{0}
\foreach [count=\n] \value/\colour/\name in {#2} {
\pgfmathparse{\value+\totalnum}
\global\let\totalnum=\pgfmathresult
\xdef\numitems{\n}}
\pgfmathsetmacro{\wheelwidth}{\pgfkeysvalueof{/donut/outer
radius}-\pgfkeysvalueof{/donut/inner radius}}
\pgfmathsetmacro{\midradius}{(\pgfkeysvalueof{/donut/outer radius}
+\pgfkeysvalueof{/donut/inner radius})/2}
\path (-\pgfkeysvalueof{/donut/outer radius}-\pgflinewidth/2,
-\pgfkeysvalueof{/donut/outer radius}-\pgflinewidth/2) coordinate(bl)
(\pgfkeysvalueof{/donut/outer radius}+\pgflinewidth/2,
\pgfkeysvalueof{/donut/outer radius}+\pgflinewidth/2) coordinate (tr);
\begin{scope}
\pgfmathsetmacro{\cumnum}{0}
\foreach \value/\colour/\name in {#2} {
\pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}
\pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}
\begin{scope}[on background layer]
\clip (bl) rectangle (tr);
\filldraw[draw=white,fill=\colour,#1]
(-\cumnum:\pgfkeysvalueof{/donut/outer radius})
arc(-\cumnum:-(\newcumnum):\pgfkeysvalueof{/donut/outer radius}) --
(-\newcumnum:\pgfkeysvalueof{/donut/inner radius})
arc(-\newcumnum:-(\cumnum):\pgfkeysvalueof{/donut/inner radius}) -- cycle;
\end{scope}
\ifx\name\empty
\else
\draw node [text=\pgfkeysvalueof{/donut/text color},
font=\bfseries\sffamily] at
(\midangle:{\pgfkeysvalueof{/donut/inner radius}+\wheelwidth/2}) {\name};
\fi
\global\let\cumnum=\newcumnum}
\end{scope}
\end{tikzpicture}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}
\centering
\begin{minipage}{0.45\textwidth}
\centering
\donutchart[rotate=45,]{1/NR/, 1/NO/, 20/NY/, 5/NG/, 73/NB/}
\caption{Caption 1}
\end{minipage}\hfill
\begin{minipage}{0.45\textwidth}
\centering
\donutchart[rotate=45]{1/NR/, 1/NO/, 20/NY/, 5/NG/, 73/NB/}
\caption{Caption 2}
\end{minipage}\hfill
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}
\centering
\begin{minipage}{0.45\textwidth}
\centering
\includegraphics[width=1\textwidth]{example-image-a}
\caption{Caption 3}
\end{minipage}\hfill
\begin{minipage}{0.45\textwidth}
\centering
\includegraphics[width=1\textwidth]{example-image-b}
\caption{Caption 4}
\end{minipage}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}