在子图中对齐调整大小的 tikzpicture

在子图中对齐调整大小的 tikzpicture

我想沿 y 轴缩放一些 tikzpicture 的子图以节省一些空间。但缩放后图片不再对齐。我想缩放它们以节省垂直空间。我不想直接缩放轴的高度,因为这样(自动)小刻度就会消失,(顺便说一句,我也希望它们在右图中)。我拥有的是

我拥有的

我想要的是(或类似)

我想要的是

我的代码是

\documentclass{article}
\usepackage{subcaption}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{tikz}
\pgfplotsset{compat=newest}
\begin{filecontents}{tabelle1.tex}
4   36.56   26.68
128 24298.04 300000
\end{filecontents}
\begin{filecontents}{tabelle2.tex}
4   24004.4 24004.4
64  240034.08   251182.32
\end{filecontents}
\begin{document}
\hrule
\begin{figure}[tbph]
    \centering
    \begin{subfigure}[t]{0.45\textwidth}
        \centering
        \resizebox{1\textwidth}{!}{% %I want to them to fit in their subfigure
            \begin{tikzpicture}
                \footnotesize
                \begin{axis}[
                    yscale=0.7 % needed to reduce vertical space
                    ,height=\textwidth % fixed height and width for both, should lead to same scaling 
                    ,width=\textwidth % and therefore same size in the end, letting them sit together nicely
                    ,xtick=data
                    ,axis y line=left
                    ,ymin=10
                    ,ymode=log 
                    ,legend style={at={(1,0.1)},anchor=south east} % turn this off and its somewhere it should not be
                    ]                   
                    \addplot table [x index=0,y index=1] {tabelle1.tex};\addlegendentry{A}
                    \addplot table [x index=0,y index=2] {tabelle1.tex};\addlegendentry{B}
                \end{axis}
        \end{tikzpicture}
        }
        \caption{a caption}
    \end{subfigure}%
    \hfill%
    \begin{subfigure}[t]{0.45\textwidth}
        \centering
        \resizebox{1\textwidth}{!}{%
            \begin{tikzpicture}
                \footnotesize
                \begin{axis}[
                    yscale=0.7
                    ,height=\textwidth
                    ,width=\textwidth
                    ,xtick=data
                    ,axis y line=left
                    ,ymin=10000
                    ,ymode=log
                    ,legend style={at={(1,0.1)},anchor=south east}
                    ]
                    \addplot table [x index=0,y index=1] {tabelle2.tex};\addlegendentry{A}
                    \addplot table [x index=0,y index=2] {tabelle2.tex};\addlegendentry{B}
                \end{axis}
        \end{tikzpicture}
        }
        \caption{b caption}     
    \end{subfigure}
    \caption{main caption}
\end{figure}
\end{document}

到目前为止,我发现和之间似乎存在某种联系yscale。关闭或打开可以使其工作。有时确实如此,找不到规则。目标编译时没有。我想要所有这三个。准确地说:提供相同结果的东西。ymodelegend styleyscaleymodelegend styleyscale

我如何实现这个目标?

我还有更多类似的人物,所以如果我能从中学到一些东西,那就更好了。如果有必要,重写所有内容也可以。

答案1

编辑:
添加了 y 轴标签并更正确地采用图表宽度到\subfigures 的宽度。

而不是缩放图片,最好使其宽度适应宽度subfigure

\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{subcaption}
\usepackage{pgfplots}
%\usepgfplotslibrary{groupplots}

\pgfplotsset{compat=1.18}
    \begin{filecontents}{tabelle1.tex}
    4   36.56   26.68
    128 24298.04 300000
    \end{filecontents}
    \begin{filecontents}{tabelle2.tex}
    4   24004.4 24004.4
    64  240034.08   251182.32
    \end{filecontents}

\begin{document}
    \begin{figure}[tbph]
\pgfplotsset{
    height=6cm, 
    width = \linewidth,     % <---
    xtick = data,
%    axis y line=left,      % <--- if you liked
    legend pos=south east,
            }
\begin{subfigure}[t]{0.5\textwidth}\raggedright
    \begin{tikzpicture}
\begin{semilogyaxis}[
    ymin=10,
    ylabel = some text,
                    ]
\addplot table [x index=0,y index=1] {tabelle1.tex};
\addplot table [x index=0,y index=2] {tabelle1.tex};
    \legend{A, B}
\end{semilogyaxis}
    \end{tikzpicture}
\caption{subfigure A caption}
\end{subfigure}
    \hfil
\begin{subfigure}[t]{0.5\textwidth}\raggedleft
    \begin{tikzpicture}
\begin{semilogyaxis}[
    ymin=10000,
    ylabel = some text,
                    ]
\addplot table [x index=0,y index=1] {tabelle2.tex};
\addplot table [x index=0,y index=2] {tabelle2.tex};
    \legend{A, B}
\end{semilogyaxis}
    \end{tikzpicture}
\caption{subfigure B caption}
\end{subfigure}%
\caption{Main caption}
    \end{figure}
\end{document}

在此处输入图片描述

(红线表示文本框边框)

答案2

使用 2 列 x 1 行的组来对齐图形。

子标题被作为节点插入,以便于配置和定位。

b

\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usepgfplotslibrary{groupplots}

\usepackage{caption,subcaption}  

\usepackage{pgfplotstable}
\usepackage{tikz}

\begin{filecontents}{tabelle1.tex}
    4   36.56   26.68
    128 24298.04 300000
\end{filecontents}
\begin{filecontents}{tabelle2.tex}
    4   24004.4 24004.4
    64  240034.08   251182.32
\end{filecontents}

\begin{document}
    
    \begin{figure}[!htp]
        \centering\footnotesize
        \begin{tikzpicture}
            \begin{groupplot}[
                group style={
                    group name=my plots,
                    group size= 2 by 1,
                    horizontal sep =2.0cm,
                },
            legend style={at={(1.0,0.05)},anchor=south east},
            height=0.45\textwidth,
            width=0.45\textwidth,
            xtick=data,
            axis y line=left,
            ymode=log
                ]
                \nextgroupplot[ymin=10]                   
                    \addplot table [x index=0,y index=1] {tabelle1.tex};\addlegendentry{A}
                    \addplot table [x index=0,y index=2] {tabelle1.tex};\addlegendentry{B}
                
                \nextgroupplot[ymin=10000]
                \addplot table [x index=0,y index=1] {tabelle2.tex};\addlegendentry{A}
                \addplot table [x index=0,y index=2] {tabelle2.tex};\addlegendentry{B}
            \end{groupplot}
    
            \tikzset{SubCaption/.style={
                    text width=0.4\textwidth,
                    yshift=-3mm, 
                    align=center,anchor=north
            }}
            
            \node[SubCaption] at (my plots c1r1.south) {\subcaption{First graph a}\label{subplot:one}};         
            \node[SubCaption] at (my plots c2r1.south) {\subcaption{Second graph b}\label{subplot:two}};

        \end{tikzpicture}
        \caption{Main caption}\label{fig:plots}
    \end{figure}
    
\end{document}

我认为你不需要 y 轴的箭头。使用axis y line*=left,你将得到

C

相关内容