0

This is my first time working with side-by-side pgfplots, so I am a little out of my zone of comfort. However, I noticed that my subcaptions for each panel is not centered around the figure, as in:

enter image description here

For panel (a), it seems like it's centered - but I would prefer a centering like the x-labels, where they are centered around what's visible on the figure - and not any whitespacing (if that makes sense). However, for panel (b), I cannot get the subcaption to be centered, even though I have used the same code.

My code is the following:

\usepackage{pgfplots, pgfplotstable}
\usepackage{subcaption}
\documentclass[]{bachelor}
\LoadClass[a4paper]{report}    
\begin{figure}[!h]
        \centering
    \begin{subfigure}{.475\linewidth}
            \centering
            \pgfplotsset{scaled x ticks=false}
            \caption{Peer rank vs. \\ Average Annual Search Fraction}
        \begin{tikzpicture}
            \begin{axis}[width=7cm,height=10cm,
            ylabel={Peer Rank},
            xlabel={Average Annual Search Fraction},
            xmin=0,
            xmax=4,
            ymin=-5,
            ymax=100,
            xtick={0,1,2,3,4},
            axis background/.style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            xtick style={draw=none},
            xticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
            ]

            \addplot table [x=c, y=b, col sep=comma] {Datafiles/rankvsasf1.csv};

            \end{axis}

        \end{tikzpicture}

    \end{subfigure}
        \hfill

    \begin{subfigure}{.475\linewidth}
            \centering
            \caption{Log Peer Rank vs. \\ Log average Annual Search Fractions}
        \begin{tikzpicture}
            \begin{axis}[width=7cm,height=10cm,
            ylabel={Log Peer Rank},
            xlabel={Log Average Annual Search Fraction},
            xmin=-6,
            xmax=-2,
            ymin=-0,
            ymax=4,
            xtick={-6,-5,-4,-3,-2},
            axis background/.style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            xtick style={draw=none},
        ]

            \addplot table [x=x, y=y, col sep=comma] {Datafiles/loglog.csv};

            ]

      \draw[->,shorten >=1mm,shorten <=1mm] (rel axis cs:0.45,0.9) node[right,draw,align=left] {y=-6.38-1.63x\\ $R^2$=99.7\%};


            \end{axis}

        \end{tikzpicture}

    \end{subfigure}
\end{figure}
  • 1
    Please, can you edit your MWE: missing \documentclass. Thank you. – Sebastiano May 3 '19 at 11:49
  • The first one seems to be centered w.r.t. to the full tikzpicture and as for the second one, probably the axis is too wide to fit. – user121799 May 3 '19 at 11:51
  • Are there any warnings about overfull boxes? – user188004 May 3 '19 at 12:06
  • I added the documentclass. There are no warnings about overfull boxes, even adjusting the width does not help. – Philip May 3 '19 at 12:10
  • Your code is far from compilable. Apart from the fact that bachelor does not seem to be a standard class, there are no \begin{document} and \end{document}. When one fixes this, one can still not compile because we do not have your data. Please provide us with a compilable code using only classes that are on the standard installations, or give us links to the bachelor class. – user121799 May 3 '19 at 12:19
1

This solution puts the caption inside a node in the tikzpicture, using only the width of the axis.

I tried centering the tikzpicture relative to the axis, but it wasted too much space on the right.

\documentclass[a4paper]{report}    
\usepackage[margin=2cm,showframe]{geometry}% default not wide enough
\usepackage{pgfplots, pgfplotstable}
\usepackage{subcaption}
\captionsetup[subfigure]{justification=centering, singlelinecheck=false}

\newlength{\tempwidth}

\begin{document}
\begin{figure}[htb]% !h is just embarrassing.
        \centering
    \begin{subfigure}{.475\linewidth}
            \centering
            \pgfplotsset{scaled x ticks=false}
        \begin{tikzpicture}
            \begin{axis}[width=7cm,height=10cm,
            ylabel={Peer Rank},
            xlabel={Average Annual Search Fraction},
            xmin=0,
            xmax=4,
            ymin=-5,
            ymax=100,
            xtick={0,1,2,3,4},
            axis background/.style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            xtick style={draw=none},
            xticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
            name=border
            ]
            %\addplot table [x=c, y=b, col sep=comma] {Datafiles/rankvsasf1.csv};% file not found
            \end{axis}
        %place caption above axis
            \pgfextractx{\tempwidth}{\pgfpointdiff{\pgfpointanchor{border}{west}}{\pgfpointanchor{border}{east}}}%
            \node[above] at (border.north) {\begin{minipage}{\tempwidth}
              \caption{Peer rank vs. \\ Average Annual Search Fraction}
            \end{minipage}};
        \end{tikzpicture}

    \end{subfigure}\hfill
    \begin{subfigure}{.475\linewidth}
            \centering

        \begin{tikzpicture}
            \begin{axis}[width=7cm,height=10cm,
            ylabel={Log Peer Rank},
            xlabel={Log Average Annual Search Fraction},
            xmin=-6,
            xmax=-2,
            ymin=-0,
            ymax=4,
            xtick={-6,-5,-4,-3,-2},
            axis background/.style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            xtick style={draw=none},
            name=border
        ]
            %\addplot table [x=x, y=y, col sep=comma] {Datafiles/loglog.csv};% file not found
            ]
      \draw[->,shorten >=1mm,shorten <=1mm] (rel axis cs:0.45,0.9) node[right,draw,align=left] {y=-6.38-1.63x\\ $R^2$=99.7\%};
            \end{axis}
        %place caption above axis
            \pgfextractx{\tempwidth}{\pgfpointdiff{\pgfpointanchor{border}{west}}{\pgfpointanchor{border}{east}}}%
            \node[above] at (border.north) {\begin{minipage}{\tempwidth}
              \caption{Log Peer Rank vs. \\ Log average Annual Search Fractions}
            \end{minipage}};
        \end{tikzpicture}

    \end{subfigure}
\end{figure}
\end{document}
| improve this answer | |
1

Here is a modified version of John's answer. Instead of measuring the width of the axis, you can use text width=\pgfkeysvalueof{/pgfplots/width}, and instead of a separate node, I add the caption in the title of the axis.

text width=\pgfkeysvalueof{/pgfplots/width} actually makes the title node a bit wider than the axis itself, because twice the inner sep of the node comes in addition, and because the width of the axis box is 45pt less than the defined width when scale only axis is not included.

I also show a different method for making the general layout, using a groupplot instead of two subfigure environments. It shortens the code a bit. Note I used \subcaption instead of \caption in the titles.

\documentclass[a4paper]{report}    
\usepackage[margin=2cm,showframe]{geometry}% default not wide enough
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots} % <-- added
\usepackage{subcaption}
\captionsetup[subfigure]{justification=centering, singlelinecheck=false}

\newlength{\tempwidth}

\begin{document}
\begin{figure}[htb]
        \centering
    \begin{subfigure}{.475\linewidth}
            \centering
            \pgfplotsset{scaled x ticks=false}
        \begin{tikzpicture}
            \begin{axis}[width=7cm,height=10cm,
            ylabel={Peer Rank},
            xlabel={Average Annual Search Fraction},
            xmin=0,
            xmax=4,
            ymin=-5,
            ymax=100,
            xtick={0,1,2,3,4},
            axis background/.style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            xtick style={draw=none},
            xticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
            title style={text width=\pgfkeysvalueof{/pgfplots/width}},% this is actually a bit wider than the axis
            title=\caption{Peer rank vs. \\ Average Annual Search Fraction}
            ]
            %\addplot table [x=c, y=b, col sep=comma] {Datafiles/rankvsasf1.csv};% file not found
            \end{axis}

        \end{tikzpicture}

    \end{subfigure}\hfill
    \begin{subfigure}{.475\linewidth}
            \centering

        \begin{tikzpicture}
            \begin{axis}[width=7cm,height=10cm,
            ylabel={Log Peer Rank},
            xlabel={Log Average Annual Search Fraction},
            xmin=-6,
            xmax=-2,
            ymin=-0,
            ymax=4,
            xtick={-6,-5,-4,-3,-2},
            axis background/.style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            xtick style={draw=none},
            title style={text width=\pgfkeysvalueof{/pgfplots/width}},
            title=\caption{Log Peer Rank vs. \\ Log average Annual Search Fractions}
        ]
            %\addplot table [x=x, y=y, col sep=comma] {Datafiles/loglog.csv};% file not found
            ]
      \draw[->,shorten >=1mm,shorten <=1mm] (rel axis cs:0.45,0.9) node[right,draw,align=left] {$y=-6.38-1.63x$\\ $R^2=99.7\%$};
            \end{axis}
        \end{tikzpicture}

     \end{subfigure}
\end{figure}

\begin{figure}[htb]
\centering
\begin{tikzpicture}
    \begin{groupplot}[
       group style={
         group size={2 by 1},
         horizontal sep=2cm
       },  
       width=7cm,height=10cm,
       axis background/.style={fill=gray!5},
       xtick pos=lower,ytick pos=left,
       xtick style={draw=none},
       title style={text width=\pgfkeysvalueof{/pgfplots/width}}, % this is actually a bit wider than the axis
       ]

       \nextgroupplot[
            ylabel={Peer Rank},
            xlabel={Average Annual Search Fraction},
            xmin=0,
            xmax=4,
            ymin=-5,
            ymax=100,
            scaled x ticks=false,
            xtick={0,1,2,3,4},
            xticklabel={\pgfmathprintnumber[/pgf/number format/.cd,fixed,precision=0,zerofill]{\tick}\%},
            title=\subcaption{Peer rank vs. \\ Average Annual Search Fraction}
            ]

            %\addplot table [x=c, y=b, col sep=comma] {Datafiles/rankvsasf1.csv};% file not found

       \nextgroupplot[
            ylabel={Log Peer Rank},
            xlabel={Log Average Annual Search Fraction},
            xmin=-6,
            xmax=-2,
            ymin=-0,
            ymax=4,
            xtick={-6,-5,-4,-3,-2},
            xtick style={draw=none},
            title=\subcaption{Log Peer Rank vs. \\ Log average Annual Search Fractions}
        ]
            %\addplot table [x=x, y=y, col sep=comma] {Datafiles/loglog.csv};% file not found
            ]
      \draw[->,shorten >=1mm,shorten <=1mm] (rel axis cs:0.45,0.9) node[right,draw,align=left] {$y=-6.38-1.63x$\\ $R^2=99.7\%$};
            \end{groupplot}
        \end{tikzpicture}



\end{figure}
\end{document}
| improve this answer | |

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.