如何使独立的 pgsplots 以 y 轴为中心?

如何使独立的 pgsplots 以 y 轴为中心?

我有一份standalone包含两个图的文档。请参阅 MWE:

\documentclass[tikz]{standalone}

% PACKAGES LOADING

\usepackage{pgfplots} % To draw plots.

% TIKZ & PGFPLOTS LIBRARIES & SETTINGS

\pgfplotsset{compat=1.6}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{fit, calc, matrix, positioning, arrows.meta, intersections, through, backgrounds, patterns}

\pgfplotsset{compat=1.17}
\usetikzlibrary{arrows.meta,
                intersections,
                positioning}
\pgfplotsset{width=6cm, height=6cm, % <---
    axis lines=middle,
    xlabel={$\cdot$},
    xlabel style={anchor=west},
    ylabel={$u(\cdot)$},
    ylabel style={anchor=south},
    xtick=\empty,   ytick=\empty,
    clip=false,
    ylabel style={overlay},
    yticklabel style={overlay},
            }
\tikzset{
ddline/.append style = {draw=gray, densely dotted},
         tick/.style = {inner sep=2pt, font=\footnotesize, align=right}
        }

% FIGURES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

% Figure 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tikzpicture}[baseline]
\begin{axis}[
    axis x line shift={-ln(0.1)}, % <---
       xmin = 0,
     domain = 0.1:5,
    samples = 100,
    scale = 1.5
            ]
\addplot [thick] {ln(\x)};
%
\draw[ddline]   
    (0.5,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$x$} |- coordinate[pos=0.5] (ux)
    (0,{ln(0.5)})   node[tick,left] {$u(x)$};
\draw[ddline, name path=B]
    (2,{ln(0.1)}) node[tick,below,xshift=20pt]    {$px+(1-p)y$} |-
    (0,{ln(2)})   node[tick,left]     {$u(px+(1-p)y)$};
\draw[ddline]   
    (4,{ln(0.1)}) node[tick,below,yshift=-1.97pt]    {$y$} |- coordinate[pos=0.5] (uy)
    (0,{ln(4)})   node[tick,left]     {$u(y)$};
\draw[name path=A, semitransparent]   (ux) -- (uy);
\draw[name intersections={of=A and B, by=s}, ddline]
    (s) -- (s -| 1.2,1.2) node[tick,left] {};
\draw[ddline]
    (1.2,{ln(0.1)}) node[tick,below]    {$c(F,u)$} |-
    (0,{ln(1.22)})   node[tick,left]    {$pu(x)+(1-p)u(y)$};
\end{axis}
\end{tikzpicture}%

% Figure 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tikzpicture}[baseline]
\begin{axis}[
    axis x line shift={-ln(0.1)}, % <---
       xmin = 0,
     domain = 0.1:5,
    samples = 100,
    scale = 1.5,
    ]
\addplot [thick] {ln(\x)};
%
\draw[ddline]   
    (0.5,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$x-\varepsilon$} |- coordinate[pos=0.5] (ux)
    (0,{ln(0.5)})   node[tick,left] {$u(x-\varepsilon)$};
\draw[ddline, name path=B]
    (2,{ln(0.1)}) node[tick,below]    {$(b)$} |-
    (0,{ln(1.22)})   node[tick,left]     {$(a)=u(x)$};
\draw[ddline]   
    (4,{ln(0.1)}) node[tick,below,yshift=-1.97pt]    {$x+\varepsilon$} |- coordinate[pos=0.5] (uy)
    (0,{ln(4)})   node[tick,left]     {$u(x+\varepsilon)$};
\draw[name path=A, semitransparent]   (ux) -- (uy);
\draw[ddline]
    (1.2,{ln(0.1)}) node[tick,below,yshift=-1.97pt]    {$x$} |-
    (1.2,{ln(1.22)})   node[tick,left]    {};
\end{axis}
\end{tikzpicture}%

\end{document}

然后我通过以下方式将这些图包含到我的主.tex文件中:\includepraphics[]{}

\documentclass{article}

% PACKAGES LOADING

\usepackage{graphicx}

% DOCUMENT ITSELF %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{figure}
\centering
\includegraphics[page=1]{MWE 1.pdf}
\caption{Caption}
\end{figure}

\begin{figure}
\centering
\includegraphics[page=2]{MWE 1.pdf}
\caption{Caption}
\end{figure}

\end{document}

正如您在图片中看到的,这些图并没有按照y-axis我的要求通过 对齐。我见过与我的问题类似的问题,但没有一个能解决我的问题。有些答案使用了 选项trim axis,但这在文档类中效果不佳,standalone因为它会删除 左侧的所有内容y-axis。我也尝试过baselineoverlayuse as bounding box选项,但无济于事。我怎样才能按其对齐这些图y-axis

在此处输入图片描述

答案1

一种解决方法是在第二个中添加幻影节点tikzpicture

\draw (0,0)   node[tick,left] {\phantom{$pu(x)+(1-p)u(y)$}};

平均能量损失

\documentclass[tikz]{standalone}

% PACKAGES LOADING

\usepackage{pgfplots} % To draw plots.

% TIKZ & PGFPLOTS LIBRARIES & SETTINGS

\pgfplotsset{compat=1.6}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{fit, calc, matrix, positioning, arrows.meta, intersections, through, backgrounds, patterns}

\pgfplotsset{compat=1.17}
\usetikzlibrary{arrows.meta,
 intersections,
 positioning}
\pgfplotsset{width=6cm, height=6cm, % <---
 axis lines=middle,
 xlabel={$\cdot$},
 xlabel style={anchor=west},
 ylabel={$u(\cdot)$},
 ylabel style={anchor=south},
 xtick=\empty,   ytick=\empty,
 clip=false,
 ylabel style={overlay},
 yticklabel style={overlay},
}
\tikzset{
 ddline/.append style = {draw=gray, densely dotted},
 tick/.style = {inner sep=2pt, font=\footnotesize, align=right}
}

% FIGURES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
 
 % Figure 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 \begin{tikzpicture}[baseline]
 \begin{axis}[
 axis x line shift={-ln(0.1)}, % <---
 xmin = 0,
 domain = 0.1:5,
 samples = 100,
 scale = 1.5
 ]
 \addplot [thick] {ln(\x)};
 %
 \draw[ddline]   
 (0.5,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$x$} |- coordinate[pos=0.5] (ux)
 (0,{ln(0.5)})   node[tick,left] {$u(x)$};
 \draw[ddline, name path=B]
 (2,{ln(0.1)}) node[tick,below,xshift=20pt]    {$px+(1-p)y$} |-
 (0,{ln(2)})   node[tick,left]     {$u(px+(1-p)y)$};
 \draw[ddline]   
 (4,{ln(0.1)}) node[tick,below,yshift=-1.97pt]    {$y$} |- coordinate[pos=0.5] (uy)
 (0,{ln(4)})   node[tick,left]     {$u(y)$};
 \draw[name path=A, semitransparent]   (ux) -- (uy);
 \draw[name intersections={of=A and B, by=s}, ddline]
 (s) -- (s -| 1.2,1.2) node[tick,left] {};
 \draw[ddline]
 (1.2,{ln(0.1)}) node[tick,below]    {$c(F,u)$} |-
 (0,{ln(1.22)})   node[tick,left]    {$pu(x)+(1-p)u(y)$};
 \end{axis}
 \end{tikzpicture}%
 
 % Figure 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 \begin{tikzpicture}[baseline]
 \begin{axis}[
 axis x line shift={-ln(0.1)}, % <---
 xmin = 0,
 domain = 0.1:5,
 samples = 100,
 scale = 1.5,
 ]
 \addplot [thick] {ln(\x)};
 %
 \draw (0,0)   node[tick,left] {\phantom{$pu(x)+(1-p)u(y)$}};
 \draw[ddline]   
 (0.5,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$x-\varepsilon$} |- coordinate[pos=0.5] (ux)
 (0,{ln(0.5)})   node[tick,left] {$u(x-\varepsilon)$};
 \draw[ddline, name path=B]
 (2,{ln(0.1)}) node[tick,below]    {$(b)$} |-
 (0,{ln(1.22)})   node[tick,left]     {$(a)=u(x)$};
 \draw[ddline]   
 (4,{ln(0.1)}) node[tick,below,yshift=-1.97pt]    {$x+\varepsilon$} |- coordinate[pos=0.5] (uy)
 (0,{ln(4)})   node[tick,left]     {$u(x+\varepsilon)$};
 \draw[name path=A, semitransparent]   (ux) -- (uy);
 \draw[ddline]
 (1.2,{ln(0.1)}) node[tick,below,yshift=-1.97pt]    {$x$} |-
 (1.2,{ln(1.22)})   node[tick,left]    {};
 \end{axis}
 \end{tikzpicture}%
 
\end{document}

在此处输入图片描述

相关内容