4

I am having a difficult time centering subcaptions for a figure. The figure looks like the following:

enter image description here

But as you can see, the Panel A and Panel B subcaption are nowhere near being centered with respect to the figures.. I have tried adjusting the \textwidth but that has not solved the issue. The code I have been using is the following:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{subcaption}

\begin{document}

\begin{figure}[!h] 
\caption{xx}
\label{Results.Robustness.PeerGroupSize}
\captionsetup[subfigure]{justification=centering}
\begin{minipage}{0.5\textwidth}
\centering Panel A: Median of \\ absolute percentage errors
\end{minipage}%
\begin{minipage}{0.5\textwidth}
\centering Panel B: Arithmetic mean of \\ absolute percentage errors
\end{minipage} 
\smallskip

  \begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}   
\begin{axis}[width=7cm,height=6cm,
            ylabel={Absolute Percentage Error},
            xmin=3.5,
            xmax=16.5,
            ymin=10,
            ymax=30,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=0},
            ytick={10,15,20,25,30},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
             title style={yshift=-0.21cm,fill=obscureblue,text
             width=5.2cm,text=white,font=\bfseries,align=center},
             title=EV/EBITDA,
]
\typeout{\pgfkeysvalueof{/pgfplots/width}}
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(4,23.4632898)
(6,20.4912848911315)
(8,19.7941272)
(10,15.7218624120743)
(12,15.6345338)
(14,15.4406741633428)
(16,15.4401631698244)
    };
\addlegendentry{\small{SBP+SARD}}
\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(4,25.4512269)
(6,23.5460147506771)
(8,22.3693236)
(10,17.7072706410539)
(12,17.5781692)
(14,17.3986212969882)
(16,17.4559060690141)
    };
\addlegendentry{\small{GICS+SARD}}    
\end{axis}
\end{tikzpicture}
\end{subfigure}%
  \begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}   
\begin{axis}[width=7cm,height=6cm,
            xmin=3.5,
            xmax=16.5,
            ymin=15,
            ymax=35,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=0},
            ytick={15, 20, 25, 30, 35},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
             title style={yshift=-0.21cm,fill=obscureblue,text
             width=5.2cm,text=white,font=\bfseries,align=center},
             title=EV/EBITDA,
]
\typeout{\pgfkeysvalueof{/pgfplots/width}}
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(4,30.6131174198114)
(6,27.8664334441736)
(8,24.9642976036815)
(10,19.7197165671043)
(12,20.3774183287973)
(14,19.9618323030134)
(16,19.4966820355432)
    };
\addlegendentry{\small{SBP+SARD}}
\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(4,33.1199843373062)
(6,30.5860910892484)
(8,28.7186190352845)
(10,24.2360675844504)
(12,23.9748988701945)
(14,23.5838901306087)
(16,22.918033041404)
    };
\addlegendentry{\small{GICS+SARD}}    
\end{axis}
\end{tikzpicture}
\end{subfigure}
\\[3ex]
 \begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}   
\begin{axis}[width=7cm,height=6cm,
            ylabel={Absolute Percentage Error},
            xmin=3.5,
            xmax=16.5,
            ymin=10,
            ymax=30,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=0},
            ytick={10, 15, 20, 25, 30},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
             title style={yshift=-0.21cm,fill=obscureblue,text
             width=5.2cm,text=white,font=\bfseries,align=center},
             title=EV/EBIT,
]
\typeout{\pgfkeysvalueof{/pgfplots/width}}
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(4,24.190563764574)
(6,23.4632898)
(8,19.4770707)
(10,15.7218624120743)
(12,15.3981318040823)
(14,15.3189028455693)
(16,15.2300002750468)
    };
\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(4,26.1805637429578)
(6,25.4512269)
(8,22.1033621)
(10,17.7528651760113)
(12,17.8042766446753)
(14,17.6018509482883)
(16,17.5561665659589)
    };
\end{axis}
\end{tikzpicture}
\end{subfigure}%
 \begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}   
\begin{axis}[width=7cm,height=6cm,
            xmin=3.5,
            xmax=16.5,
            ymin=15,
            ymax=35,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=0},
            ytick={15, 20, 25, 30, 35},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
             title style={yshift=-0.21cm,fill=obscureblue,text
             width=5.2cm,text=white,font=\bfseries,align=center},
             title=EV/EBIT,
]
\typeout{\pgfkeysvalueof{/pgfplots/width}}
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(4,30.1828945498625)
(6,30.6131174198114)
(8,24.7747031767237)
(10,19.7197165671043)
(12,19.1663236491184)
(14,18.8627815830674)
(16,18.548416507874)
    };
\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(4,32.4846302346739)
(6,33.1199843373062)
(8,27.7291528562977)
(10,22.5727172210159)
(12,22.4200373120666)
(14,22.0372230131445)
(16,21.8049523810071)
    };
\end{axis}
\end{tikzpicture}
\end{subfigure}
\\[3ex]
 \begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}   
\begin{axis}[width=7cm,height=6cm,
            ylabel={Absolute Percentage Error},
            xmin=3.5,
            xmax=16.5,
            ymin=15,
            ymax=35,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=0},
            ytick={15,20,25,30,35},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
             title style={yshift=-0.21cm,fill=obscureblue,text
             width=5.2cm,text=white,font=\bfseries,align=center},
             title=P/E,
]
\typeout{\pgfkeysvalueof{/pgfplots/width}}
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(4,27.2405585033126)
(6,26.4912848911315)
(8,25.3931816)
(10,20.1458041214363)
(12,19.291469183723)
(14,19.041479576969)
(16,19.4927359925628)
    };

\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(4,28.9744076009443)
(6,28.2967998061087)
(8,26.6487741)
(10,20.3741607827884)
(12,20.7968307400288)
(14,20.6056723271187)
(16,20.7669914784084)
    };
\end{axis}
\end{tikzpicture}
\end{subfigure}%
 \begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}   
\begin{axis}[width=7cm,height=6cm,
            xmin=3.5,
            xmax=16.5,
            ymin=20,
            ymax=40,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=0},
            ytick={20,25,30,35,40},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
             title style={yshift=-0.21cm,fill=obscureblue,text
             width=5.2cm,text=white,font=\bfseries,align=center},
             title=P/E,
]
\typeout{\pgfkeysvalueof{/pgfplots/width}}
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(4,33.8786615361488)
(6,31.8664334441734)
(8,28.4849253966817)
(10,27.0836861737795)
(12,25.3503999589568)
(14,25.9544593152303)
(16,25.4966820355432)
    };

\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(4,35.9456063203218)
(6,34.6421278213531)
(8,30.5464004812642)
(10,28.0957792109814)
(12,27.4009061891243)
(14,27.2852880957453)
(16,27.2918033041404)
    };
\end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}

\end{document}
  • 1
    Aren't they centered w.r.t. the full thing, i.e. not just the boxes but the full plot including labels and so on on the left. Unfortunately your code is not fully compilable, it complains about obscureblue not being defined. – user121799 May 12 at 15:08
  • Yes, the subcaption for Panel A is centered aligned, if one considers the whitespace and y-axis label. However, it does not look satisfactory and would be much better, if it was aligned w.r.t. the boxes. Note: I'll change the code to remove the raggedleft alignments. – Philip May 12 at 15:11
  • You can put a \captionof in the title of the topmost figures, this will look as you may want it to look. My problem is that, after I define obsureblue, the output does not look like what you show. – user121799 May 12 at 15:18
  • @marmot - I tried putting the '\captionof' after '\begin{tikzpicture}' and '\begin{subfigure}' for both of the top figures, but it didn't work. It essentially deleted my graph.. – Philip May 12 at 15:26
5

There is the axis description cs: which allows you to place stuff relative to the axis rectangle. This coordinate system can be used to center the texts relative to the axis rectangles.

\documentclass{article}
\usepackage[margin=1in,showframe]{geometry}

\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{subcaption}
\definecolor{obscureblue}{RGB}{39,70,182}
\begin{document}

\begin{figure}[!h] 
\caption{xx}
\label{Results.Robustness.PeerGroupSize}
\captionsetup[subfigure]{justification=centering}
% \begin{minipage}{0.5\textwidth}
% \centering Panel A: Median of \\ absolute percentage errors
% \end{minipage}%
% \begin{minipage}{0.5\textwidth}
% \centering Panel B: Arithmetic mean of \\ absolute percentage errors
% \end{minipage} 
% \smallskip

  \begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}   
\begin{axis}[width=7cm,height=6cm,
            ylabel={Absolute Percentage Error},
            xmin=3.5,
            xmax=16.5,
            ymin=10,
            ymax=30,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=0},
            ytick={10,15,20,25,30},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
             title style={yshift=-0.21cm,fill=obscureblue,text
             width=5.2cm,text=white,font=\bfseries,align=center},
             title=EV/EBITDA,
]
\typeout{\pgfkeysvalueof{/pgfplots/width}}
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(4,23.4632898)
(6,20.4912848911315)
(8,19.7941272)
(10,15.7218624120743)
(12,15.6345338)
(14,15.4406741633428)
(16,15.4401631698244)
    };
\addlegendentry{\small{SBP+SARD}}
\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(4,25.4512269)
(6,23.5460147506771)
(8,22.3693236)
(10,17.7072706410539)
(12,17.5781692)
(14,17.3986212969882)
(16,17.4559060690141)
    };
\addlegendentry{\small{GICS+SARD}}    
\coordinate (ttl) at (axis description cs:0.5,1.25) ;
\end{axis}
\node[anchor=south,text width=7cm,align=center] at (ttl) 
{Panel A: Median of \\ absolute percentage errors};
\end{tikzpicture}
\end{subfigure}%
  \begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}   
\begin{axis}[width=7cm,height=6cm,
            xmin=3.5,
            xmax=16.5,
            ymin=15,
            ymax=35,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=0},
            ytick={15, 20, 25, 30, 35},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
             title style={yshift=-0.21cm,fill=obscureblue,text
             width=5.2cm,text=white,font=\bfseries,align=center},
             title=EV/EBITDA,
]
\typeout{\pgfkeysvalueof{/pgfplots/width}}
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(4,30.6131174198114)
(6,27.8664334441736)
(8,24.9642976036815)
(10,19.7197165671043)
(12,20.3774183287973)
(14,19.9618323030134)
(16,19.4966820355432)
    };
\addlegendentry{\small{SBP+SARD}}
\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(4,33.1199843373062)
(6,30.5860910892484)
(8,28.7186190352845)
(10,24.2360675844504)
(12,23.9748988701945)
(14,23.5838901306087)
(16,22.918033041404)
    };
\addlegendentry{\small{GICS+SARD}}    
\coordinate (ttr) at (axis description cs:0.5,1.25) ;
\end{axis}
\node[anchor=south,text width=7cm,align=center] at (ttr) 
{Panel B: Arithmetic mean of \\ absolute percentage errors};
\end{tikzpicture}
\end{subfigure}
\\[3ex]
 \begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}   
\begin{axis}[width=7cm,height=6cm,
            ylabel={Absolute Percentage Error},
            xmin=3.5,
            xmax=16.5,
            ymin=10,
            ymax=30,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=0},
            ytick={10, 15, 20, 25, 30},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
             title style={yshift=-0.21cm,fill=obscureblue,text
             width=5.2cm,text=white,font=\bfseries,align=center},
             title=EV/EBIT,
]
\typeout{\pgfkeysvalueof{/pgfplots/width}}
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(4,24.190563764574)
(6,23.4632898)
(8,19.4770707)
(10,15.7218624120743)
(12,15.3981318040823)
(14,15.3189028455693)
(16,15.2300002750468)
    };
\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(4,26.1805637429578)
(6,25.4512269)
(8,22.1033621)
(10,17.7528651760113)
(12,17.8042766446753)
(14,17.6018509482883)
(16,17.5561665659589)
    };
\end{axis}
\end{tikzpicture}
\end{subfigure}%
 \begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}   
\begin{axis}[width=7cm,height=6cm,
            xmin=3.5,
            xmax=16.5,
            ymin=15,
            ymax=35,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=0},
            ytick={15, 20, 25, 30, 35},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
             title style={yshift=-0.21cm,fill=obscureblue,text
             width=5.2cm,text=white,font=\bfseries,align=center},
             title=EV/EBIT,
]
\typeout{\pgfkeysvalueof{/pgfplots/width}}
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(4,30.1828945498625)
(6,30.6131174198114)
(8,24.7747031767237)
(10,19.7197165671043)
(12,19.1663236491184)
(14,18.8627815830674)
(16,18.548416507874)
    };
\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(4,32.4846302346739)
(6,33.1199843373062)
(8,27.7291528562977)
(10,22.5727172210159)
(12,22.4200373120666)
(14,22.0372230131445)
(16,21.8049523810071)
    };
\end{axis}
\end{tikzpicture}
\end{subfigure}
\\[3ex]
 \begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}   
\begin{axis}[width=7cm,height=6cm,
            ylabel={Absolute Percentage Error},
            xmin=3.5,
            xmax=16.5,
            ymin=15,
            ymax=35,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=0},
            ytick={15,20,25,30,35},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
             title style={yshift=-0.21cm,fill=obscureblue,text
             width=5.2cm,text=white,font=\bfseries,align=center},
             title=P/E,
]
\typeout{\pgfkeysvalueof{/pgfplots/width}}
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(4,27.2405585033126)
(6,26.4912848911315)
(8,25.3931816)
(10,20.1458041214363)
(12,19.291469183723)
(14,19.041479576969)
(16,19.4927359925628)
    };

\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(4,28.9744076009443)
(6,28.2967998061087)
(8,26.6487741)
(10,20.3741607827884)
(12,20.7968307400288)
(14,20.6056723271187)
(16,20.7669914784084)
    };
\end{axis}
\end{tikzpicture}
\end{subfigure}%
 \begin{subfigure}[b]{0.5\textwidth}
\begin{tikzpicture}   
\begin{axis}[width=7cm,height=6cm,
            xmin=3.5,
            xmax=16.5,
            ymin=20,
            ymax=40,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=0},
            ytick={20,25,30,35,40},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
             title style={yshift=-0.21cm,fill=obscureblue,text
             width=5.2cm,text=white,font=\bfseries,align=center},
             title=P/E,
]
\typeout{\pgfkeysvalueof{/pgfplots/width}}
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(4,33.8786615361488)
(6,31.8664334441734)
(8,28.4849253966817)
(10,27.0836861737795)
(12,25.3503999589568)
(14,25.9544593152303)
(16,25.4966820355432)
    };

\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(4,35.9456063203218)
(6,34.6421278213531)
(8,30.5464004812642)
(10,28.0957792109814)
(12,27.4009061891243)
(14,27.2852880957453)
(16,27.2918033041404)
    };
\end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}

\end{document}

enter image description here

I used the geometry package to make the page wide enough and show the page frame (which you can of course undo by dropping showframe). As long as the nodes have a key like text width they also can host \captionof commands.

  • That is exactly what I was looking for, thank you very much! A minor thing: Originally, I wanted to use '\raggedleft' to make sure Panel B was aligned to the right margin of the page. I am trying to center the entire figure environment, but putting '\centering' after '\begin{figure}' did not solve it. Is there another way? – Philip May 12 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.