Pgfplots 不使用外部刻度标签或更改默认字体

Pgfplots 不使用外部刻度标签或更改默认字体

tick align=outside我对 pgfplots 中和的奇怪行为感到困惑\ttfamily。这是我的代码:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{fillbetween}
\tikzset{every picture/.style={/utils/exec={font=\ttfamily}}}

\begin{document}
    \pgfplotsset{
        width=15cm, height=7cm, compat=1.3
    }

\definecolor{fillColor}{RGB}{125, 185, 250}
    \pgfmathdeclarefunction{gauss}{2}{%
        \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
    }
    
    \begin{tikzpicture}
        \begin{axis}[axis lines = left,
            tick align=outside,
            every axis x label/.style={at={(current axis.right of origin)},anchor=north east},
            every axis y label/.style={at={(current axis.north west)},above=0.5mm},
            xlabel=$x$,
            ylabel=$f(x)$,
            %ylabel style={rotate=-90},
            mark=none,
            domain=-1.9:7,
            samples=100,
            smooth, % All plots: from -2:2, 50 samples, smooth, no marks
            axis lines=left, % the * suppresses the arrow tips
            enlargelimits=upper] % extend the axes a bit to the right and top
            \path [name path=B]
            (\pgfkeysvalueof{/pgfplots/xmin},0) --
            (\pgfkeysvalueof{/pgfplots/xmax},0);
            \addplot [color=fillColor!125, name path=A] {gauss(0,0.5)};
            \addplot [fillColor!50] fill between [of = A and B];
            \node at (axis cs:1,0.8) {$\mathcal{N}(0,0.5)$};
            \addplot [color=fillColor!100, name path=C] {gauss(2,0.75)};
            \addplot [fillColor!25] fill between [of = C and B];
            \node at (axis cs:3.,0.55) {$\mathcal{N}(2,0.75)$};
            \addplot [color=fillColor!75, name path=D] {gauss(4,1)};
            \addplot [fillColor!10] fill between [of = D and B];
            \node at (axis cs:5.,0.4) {$\mathcal{N}(4,1)$};
        \end{axis}
    \end{tikzpicture}
\end{document}

这让我得到了这个数字:

在此处输入图片描述

但我不明白为什么刻度线不在轴外,以及为什么字体不是 \ttfamily。欢迎提出任何想法!

答案1

  • 如果您使用的是旧版本pgfplots,我强烈建议您将其更新至最新版本 1.18。
  • 图表轴声明了两次。删除第二个。 \pgfplotsset(参见下面的 MWE)
  • 看看以下 MWE 是否给出了您想要的结果:
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.18,
             width=15cm, height=7cm,
             %ticklabel style = {font=\ttfamily}
             }

\begin{document}

\definecolor{fillColor}{RGB}{125, 185, 250}
    \pgfmathdeclarefunction{gauss}{2}{%
        \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
    }

    \begin{tikzpicture}
        \begin{axis}[
        axis lines = left,
            tick align=outside,
            xlabel=$x$,     xlabel style={at ={(1,0)},anchor=north east},
            ylabel=$f(x)$,  ylabel style={at ={(0,1)}, rotate=270, anchor=south},
            mark=none,
            domain=-1.9:7,
            samples=100,
            smooth, % All plots: from -2:2, 50 samples, smooth, no marks
            enlargelimits=upper] % extend the axes a bit to the right and top
            \path [name path=B]
            (\pgfkeysvalueof{/pgfplots/xmin},0) --
            (\pgfkeysvalueof{/pgfplots/xmax},0);
            \addplot [color=fillColor!125, name path=A] {gauss(0,0.5)};
            \addplot [fillColor!50] fill between [of = A and B];
            \node at (axis cs:1,0.8) {$\mathcal{N}(0,0.5)$};
            \addplot [color=fillColor!100, name path=C] {gauss(2,0.75)};
            \addplot [fillColor!25] fill between [of = C and B];
            \node at (axis cs:3.,0.55) {$\mathcal{N}(2,0.75)$};
            \addplot [color=fillColor!75, name path=D] {gauss(4,1)};
            \addplot [fillColor!10] fill between [of = D and B];
            \node at (axis cs:5.,0.4) {$\mathcal{N}(4,1)$};
        \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容