y 轴和标签的位置

y 轴和标签的位置
\documentclass[a4paper,11pt,fleqn]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgf,tikz}
\usepackage{graphicx}
\usepackage{array}
\usepackage[ngerman]{babel}
\usepackage{gauss}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{positioning,fit,calc}

\pgfmathdeclarefunction{gauss}{2}{\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}

\begin{document}

\begin{figure}
    \centering
\begin{tikzpicture}
\begin{axis}[scale only axis,
         axis lines =middle,
                     ylabel style = {align=left},
         inner axis line style={=>},
                 width=10cm,height=6cm,
                     ymin=0,ymax=0.45,
                     xmin=496,xmax=504,
                     axis line style = thick,
                     xtick={497,498,499,500,501,502,503},
                     ytick={0.1,0.2,0.3,0.4},
                 every axis x label/.style={at={(current axis.right of origin)},anchor=west},
         every axis y label/.style={at={(current axis.north)},above=0.5mm},
                 xlabel={$x$},
                 ylabel={$f(x)$},
                     axis on top]
\addplot [domain=496:504,samples=500,red,thick] {gauss(500,16/16)};
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

我的问题:尝试制作两个不同的情节

  1. 首先绘制 y 轴应与 x 轴在 496 处相交,因此在左侧,标签应位于 y 轴的顶部(图片)

  2. 第二幅图的 y 轴应与 x 轴在中间(500)处相交,并且标签应位于顶部

在此处输入图片描述

答案1

如果我理解正确的话,您正在寻找:

在此处输入图片描述

上图的代码基于我的答案对于你之前的问题:

\documentclass[a4paper,11pt,fleqn]{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\pgfmathdeclarefunction{gauss}{2}{%
    \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
                                }

\begin{document}
    \begin{figure}[htb]
    \centering
\begin{tikzpicture}% left picture
\begin{axis}[
    width=6cm,height=6cm,
        scale only axis,
        axis lines=center,
        ymin=0,ymax=0.45,
        axis line style = thick,
        xtick={500,501,502,503},
            extra x ticks={500},
            extra x tick label={500},
        x label style={anchor=west},
        y label style={anchor=south},
        xlabel={$x$},
        ylabel={$f(x)$},
        axis on top,
        samples=50]
\addplot [domain=500:504,red,thick] {gauss(500,16/16)};
\end{axis}
\end{tikzpicture}
    \hfill        
\begin{tikzpicture}% right picture
\begin{axis}[
    width=6cm,height=6cm,
        scale only axis,
        axis lines=center,
        xmin=-4,xmax=4,
        ymin=0,ymax=0.45,
        axis line style = thick,
        xtick={-3,-2,-1,1,2,3},
        xticklabels={497,...,503},
            extra x ticks={0},
            extra x tick label={500},
        x label style={anchor=west},
        y label style={anchor=south},
        xlabel={$x$},
        ylabel={$f(x)$},
        axis on top,
        samples=50]
\addplot [domain=-3:3,red,thick] {gauss(0,1)};
\end{axis}
\end{tikzpicture}
    \end{figure}
\end{document}

编辑: 通过定义公共轴特征\pgfplotsset您将获得更短的代码(给出与姆韦多于):

\documentclass[a4paper,11pt,fleqn]{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\pgfmathdeclarefunction{gauss}{2}{%
    \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
                                }

\begin{document}
    \begin{figure}[htb]
    \centering
\pgfplotsset{width=6cm,height=6cm,
             scale only axis,
             axis lines=middle,
             ymin=0,ymax=0.45,
             axis line style = thick,
             xlabel={$x$},
             ylabel={$f(x)$},
             x label style={anchor=west},
             y label style={anchor=south},
             axis on top,
             samples=50}
        \begin{tikzpicture}% left picture
\begin{axis}[
    xtick={500,501,502,503},
    extra x ticks={500},
    extra x tick label={500}
                ]
\addplot [domain=500:504,red,thick] {gauss(500,16/16)};
\end{axis}
\end{tikzpicture}
    \hfill
\begin{tikzpicture}% right picture
\begin{axis}[
    xmin=-4,xmax=4,
    xtick={-3,-2,-1,1,2,3},
    xticklabels={497,...,503}
            ]
\addplot [domain=-3:3,red,thick] {gauss(0,1)};
\end{axis}
\end{tikzpicture}
    \end{figure}
\end{document}

答案2

您的问题没有明确说明您想要什么,两个图是否在同一个tikzpicture坐标系中,还是在单独的坐标系中。

y-axis无论如何,如果情况相同,只需在原始情节中绘制额外内容即可减少问题。

我发布了两种替代方案:

两个独立的地块

在此处输入图片描述

\documentclass[a4paper,11pt,fleqn]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgf,tikz}
\usepackage{graphicx}
\usepackage{array}
\usepackage[ngerman]{babel}
\usepackage{gauss}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{positioning,fit,calc}

\pgfmathdeclarefunction{gauss}{2}{\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}

\begin{document}

% First Plot
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[%
        scale only axis,
        axis lines = middle,
        ylabel style = {align=left},
        inner axis line style={=>},
        width=6cm, height=6cm,
        ymin=0,ymax=0.45,
        xmin=500,xmax=504,
        axis line style = thick,
        xtick={500,501,502,503},
        ytick={0.1,0.2,0.3,0.4},
        every axis x label/.style={at={(current axis.right of origin)},anchor=west},
        every axis y label/.style={at={(current axis.north west)}, above=0.5mm},
        xlabel={$x$},
        ylabel={$f(x)$},
        ]
\addplot [domain=500:504,samples=500,red,thick] {gauss(500,16/16)};
\end{axis}
\end{tikzpicture}

% Second Plot

\begin{tikzpicture}
\begin{axis}[%
        scale only axis,
        axis lines =middle,
        ylabel style = {align=left},
        inner axis line style={=>},
        width=10cm,height=6cm,
        ymin=0,ymax=0.45,
        xmin=496,xmax=504,
        axis line style = thick,
        xtick={497,498,499,500,501,502,503},
        ytick={0.1,0.2,0.3,0.4},
        every axis x label/.style={at={(current axis.right of origin)},anchor=west},
        every axis y label/.style={at={(current axis.north west)}, above=0.5mm},
        xlabel={$x$},
        ylabel={$f(x)$},
        axis on top
        ]
\addplot [domain=496:504,samples=500,red,thick] {gauss(500,16/16)};
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

一个图中有两个 y 轴。

在此处输入图片描述

\begin{tikzpicture}
\begin{axis}[%
        scale only axis,
        axis lines =middle,
        ylabel style = {align=left},
        inner axis line style={=>},
        width=10cm,height=6cm,
        ymin=0,ymax=0.45,
        xmin=496,xmax=504,
        axis line style = thick,
        xtick={497,498,499,500,501,502,503},
        ytick={0.1,0.2,0.3,0.4},
        every axis x label/.style={at={(current axis.right of origin)},anchor=west},
        every axis y label/.style={at={(current axis.north west)}, above=1mm},
        ymajorgrids,
        xlabel={$x$},
        ylabel={$f(x)$},
        axis on top
        ]
\addplot [domain=496:504,samples=500,red,thick] {gauss(500,16/16)};
\end{axis}

\draw[-stealth, thick] (5,0)--(5,6) node[label=above:$f(x)$]{};

\end{tikzpicture}

相关内容