PGGFlots 子图并排放置

PGGFlots 子图并排放置

我无法强制两个 PGFPlot 并排显示,有人可以修复我的编码吗?如果可能的话,我还想更好地将子标题居中。我剪切了大部分数据以适应这个空间。提前谢谢。

\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}

编译输出

答案1

正如 Torbjørn T. 在问题下方的评论中指出的那样,您提供的代码中存在一些“错误”。在此回答中,我只是用完整的代码示例总结了这些错误,并提供了两种建议的解决方案。

(这就是为什么现在两个图的大小不一样的原因。只使用其中一个解决方案,它们将要有相同的大小。

有关更多详细信息,请查看代码中的注释。

% 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}

该图显示了上述代码的结果

相关内容