1

I am having trouble forcing the two PGFPlots to appear side by side, can someone please fix my coding. I would also like to center the subcaptions better if possible. I cut most of the data to fit into this space. thanks in advance.

\begin{filecontents*}{ST0421.csv}
nm,0,2.3,3.5,5.2,7.8,11.7,17.6,26.3,39.5,59.3,88.9,133.3,200,300
380,0.102,0.102,0.101,0.103,0.101,0.104,0.105,0.103,0.101,0.111,0.105,0.103,0.103,0.101
381,0.105,0.104,0.104,0.106,0.105,0.105,0.103,0.103,0.103,0.113,0.109,0.106,0.107,0.106
382,0.108,0.109,0.108,0.108,0.106,0.106,0.108,0.106,0.106,0.117,0.109,0.111,0.107,0.103
383,0.103,0.105,0.105,0.105,0.103,0.104,0.104,0.104,0.105,0.111,0.105,0.105,0.105,0.102
384,0.109,0.107,0.109,0.108,0.108,0.108,0.109,0.108,0.108,0.115,0.109,0.112,0.107,0.105
385,0.109,0.11,0.109,0.111,0.108,0.11,0.112,0.11,0.112,0.12,0.112,0.11,0.112,0.108
386,0.113,0.117,0.116,0.118,0.114,0.114,0.116,0.116,0.112,0.123,0.116,0.114,0.114,0.112
387,0.119,0.121,0.119,0.12,0.12,0.119,0.124,0.12,0.119,0.125,0.12,0.12,0.12,0.117
388,0.124,0.122,0.123,0.122,0.123,0.122,0.122,0.122,0.121,0.129,0.124,0.124,0.122,0.117
389,0.131,0.127,0.13,0.131,0.128,0.128,0.133,0.13,0.126,0.135,0.13,0.127,0.128,0.124
390,0.139,0.136,0.136,0.136,0.138,0.139,0.14,0.137,0.136,0.141,0.139,0.138,0.132,0.13
391,0.143,0.143,0.142,0.143,0.138,0.143,0.144,0.141,0.141,0.147,0.145,0.14,0.143,0.135
392,0.152,0.151,0.151,0.15,0.147,0.149,0.151,0.15,0.151,0.156,0.152,0.149,0.15,0.143
393,0.154,0.15,0.155,0.153,0.15,0.153,0.153,0.153,0.154,0.158,0.153,0.151,0.151,0.144
394,0.16,0.158,0.159,0.161,0.158,0.158,0.164,0.16,0.159,0.164,0.162,0.159,0.159,0.151
\end{filecontents*}

\begin{figure}[!h]

\centering
\begin{subfigure}{.45\linewidth}\centering
\begin{tikzpicture}
\begin{axis}[
scale only axis,
width=\linewidth,
xlabel={nm},
ylabel={Abs},
xmin=385, xmax=595,
ymajorgrids=true,
grid style=dashed,
legend pos=north east,
cycle list name=color list,
legend columns=2,
legend cell align=left]

\addplot table [x=nm, y=0, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=2.3, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=3.5, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=5.2, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=7.8, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=11.7, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=17.6, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=26.3, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=39.5, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=59.3, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=88.9, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=133.3, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=200, col sep=comma] {ST0421.csv};
\addplot table [x=nm, y=300, col sep=comma] {ST0421.csv};

\legend{0 $\mu$M,2.3 $\mu$M,3.5 $\mu$M,5.2 $\mu$M,7.8 $\mu$M,11.7 $\mu$M,17.6 $\mu$M,26.3 $\mu$M,39.5 $\mu$M,59.3 $\mu$M,88.9 $\mu$M,133.3 $\mu$M,200 $\mu$M,300 $\mu$M}
\end{axis}
\end{tikzpicture}
\caption{uv spectra of st042 Por free base with Me-IMD in 5 mM buffer}
\end{subfigure}%
\hfill

\begin{filecontents*}{ST0422.csv}
nm,0,2.3,3.5,5.2,7.8,11.7,17.6,26.3,39.5,59.3,88.9,133.3,200
380,0.14,0.141,0.14,0.14,0.136,0.137,0.132,0.131,0.135,0.133,0.13,0.13,0.134
 381,0.146,0.142,0.145,0.141,0.138,0.136,0.136,0.132,0.13,0.129,0.131,0.131,0.137
382,0.145,0.143,0.144,0.145,0.141,0.139,0.134,0.134,0.13,0.13,0.132,0.134,0.136
383,0.141,0.138,0.141,0.141,0.137,0.131,0.13,0.129,0.127,0.128,0.126,0.129,0.132
384,0.145,0.144,0.144,0.14,0.135,0.136,0.132,0.131,0.13,0.128,0.128,0.129,0.134
385,0.147,0.143,0.143,0.142,0.141,0.136,0.132,0.13,0.132,0.13,0.13,0.127,0.131
\end{filecontents*}

\begin{subfigure}{.45\linewidth}\centering
\begin{tikzpicture}
\begin{axis}[
scale only axis,
width=\linewidth,
xlabel={nm},
ylabel={Abs},
xmin=385, xmax=595,
ymajorgrids=true,
grid style=dashed,
legend pos=north east,
cycle list name=color list,
legend columns=2,
legend cell align=left]

\addplot table [x=nm, y=0, col sep=comma] {ST0422.csv};
\addplot table [x=nm, y=2.3, col sep=comma] {ST0422.csv};
\addplot table [x=nm, y=3.5, col sep=comma] {ST0422.csv};
\addplot table [x=nm, y=5.2, col sep=comma] {ST0422.csv};
\addplot table [x=nm, y=7.8, col sep=comma] {ST0422.csv};
\addplot table [x=nm, y=11.7, col sep=comma] {ST0422.csv};
\addplot table [x=nm, y=17.6, col sep=comma] {ST0422.csv};
\addplot table [x=nm, y=26.3, col sep=comma] {ST0422.csv};
\addplot table [x=nm, y=39.5, col sep=comma] {ST0422.csv};
\addplot table [x=nm, y=59.3, col sep=comma] {ST0422.csv};
\addplot table [x=nm, y=88.9, col sep=comma] {ST0422.csv};
\addplot table [x=nm, y=133.3, col sep=comma] {ST0422.csv};
\addplot table [x=nm, y=200, col sep=comma] {ST0422.csv};


\legend{0 $\mu$M,2.3 $\mu$M,3.5 $\mu$M,5.2 $\mu$M,7.8 $\mu$M,11.7 $\mu$M,17.6 $\mu$M,26.3 $\mu$M,39.5 $\mu$M,59.3 $\mu$M,88.9 $\mu$M,133.3 $\mu$M,200 $\mu$M,300 $\mu$M}
\end{axis}
\end{tikzpicture}
\caption{uv spectra of st042 Por-Co Me-IMD in 5 mM buffer}
\end{subfigure}


\end{figure}

\end{document}

compiled output

Stefan Pinnow
  • 29,535
  • 1
    Empty line in code -> paragraph break. You have empty lines between the two subfigures, so they're placed in different paragraphs, and hence different lines. – Torbjørn T. Jun 26 '15 at 22:35
  • As for the centering: You have set scale only axis,width=\linewidth, which means that only the axis is \linewidth wide. Ticklabels and axis labels come in addition, so the whole thing is wider than the subfigure, and therefore it pokes a bit out on the right side. – Torbjørn T. Jun 26 '15 at 22:42
  • hi, thanks for your help. would you please suggest a good way to center the subcaptions? – Spencer Trinh Jun 27 '15 at 09:58
  • The captions are centered in their subfigures. But because of settings I mentioned above the plots are wider than the subfigures, so they stick out on the right side of the subfigures, and therefore they are not centered. You should make the plots narrower, e.g. by removing scale only axis, or by setting the width to something smaller than \linewidth. If you want to ignore the fact that it is too wide, you can use the \makebox trick in Center figure that is wider than \textwidth. Place the whole tikzpicture in the \makebox. – Torbjørn T. Jun 27 '15 at 10:12

1 Answers1

2

As Torbjørn T. already stated in the comments below the question you had a few "mistakes" in your provided code. In this answer I just summarize them with a full code example and providing both suggested solutions.

(That is the cause why now the both plots don't have the same size. Use only one of the solutions and they will have the same size.)

For more details please have a look at the comments in the code.

% used PGFPlots v1.14
\documentclass{scrartcl}
\usepackage{subcaption}
\usepackage{pgfplots}
    \pgfplotsset{
        % use this `compat' level or higher to make use of the advanced
        % axis label positionings
        compat=1.3,
    }
\begin{document}
    % (only for debugging)
    \noindent\hrulefill
\begin{figure}[!h]
        \centering
    \begin{subfigure}{.475\linewidth}
            \centering
        \begin{tikzpicture}
            \begin{axis}[
%                % either don't use `scale only axis' ...
%                scale only axis,
                % ... and you can use `width=\linewidth' ...
                width=\linewidth,
                xlabel={nm},
                ylabel={Abs},
                % (only for debugging)
                name=left plot,
            ]
            \end{axis}
                % (only for debugging)
                \draw [red]
                    (left plot.outer south west)
                        rectangle
                    (left plot.outer north east)
                ;
        \end{tikzpicture}
        \caption{uv spectra of st042 Por free base with Me-IMD in 5 mM buffer}
    \end{subfigure}%
        \hfill%
        % here was an empty line which caused that the plots where not next
        % to each other but on top of each other
    \begin{subfigure}{.475\linewidth}
            \centering
        \begin{tikzpicture}
            \begin{axis}[
                % ... or you can use `scale only axis' ...
                scale only axis,
                % ... and therefore you have to change the `width'
                width=0.8\linewidth,
                xlabel={nm},
                ylabel={Abs},
                % (only for debugging)
                name=right plot,
            ]
            \end{axis}
                % (only for debugging)
                \draw [red]
                    (right plot.outer south west)
                        rectangle
                    (right plot.outer north east)
                ;
        \end{tikzpicture}
        \caption{uv spectra of st042 Por-Co Me-IMD in 5 mM buffer}
    \end{subfigure}
\end{figure}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535