在 pgfplot 中标记小宽度

在 pgfplot 中标记小宽度

我只是想问一下如何用箭头标记条带的宽度并像 jpg 中显示的那样做标记。

xticklabel 间距也存在一些问题。它们上下移动。

我已附上完整的运行脚本。请帮忙。

问候

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[scale=1]
\tikzset{
    hatch distance/.store in=\hatchdistance,
    hatch distance=10pt,
    hatch thickness/.store in=\hatchthickness,
    hatch thickness=2pt
}

\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
    \pgfusepath{stroke}
}
\makeatother

\begin{axis}[scale only axis,
    axis lines =middle,
    hide y axis, 
    xmin=-4,xmax=4,
    x axis line style={draw opacity=1},
    ymin=0,ymax=0.5,
    xtick={-0.5,0.25},
    xticklabels={$x$, $x+\delta x$},
    axis on top,
    ] 
\addplot[ultra thick, color=orange,domain=-4:4,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)};

\addplot+[ultra thick, mark=none,
    domain=-0.5:0.25,
    samples=100,
    pattern=flexible hatch,
    area legend,
    pattern color=red]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
\end{axis}

\结束{tikzpicture} \结束{文档} 在此处输入图片描述

答案1

您可以使用 绘制箭头axis direction cs。请注意,我还切换到了patterns.meta

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns.meta}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[scale=1]

\begin{axis}[scale only axis,
    axis lines =middle,
    hide y axis, 
    xmin=-4,xmax=4,
    x axis line style={draw opacity=1},
    ymin=0,ymax=0.5,
    xtick={-0.5,0.25},
    xticklabels={$x\vphantom{\delta}$, $x+\delta x$},
    axis on top,
    ] 
\addplot[ultra thick, color=orange,domain=-4:4,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)};

\addplot+[ultra thick, mark=none,
    domain=-0.5:0.25,
    samples=100,
    pattern={Lines[angle=45,distance={5pt}]},
    area legend,
    pattern color=red]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
    \draw[stealth-,thick] (0.25,0.1) -- ++ (axis direction cs:0.5,0)
    node[above]{$\delta x$};
    \draw[stealth-,thick] (-0.5,0.1) -- ++ (axis direction cs:-0.5,0);
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

您也可以使用fillbetween此处的图书馆。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns.meta}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[scale=1]

\begin{axis}[scale only axis,
    axis lines =middle,
    hide y axis, 
    xmin=-4,xmax=4,
    x axis line style={draw opacity=1},
    ymin=0,ymax=0.5,
    xtick={-0.5,0.25},
    xticklabels={$x\vphantom{\delta}$, $x+\delta x$},
    axis on top,
    ] 
\addplot[name path=A,ultra thick, color=orange,domain=-4:4,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)};
\path [name path=B] (\pgfkeysvalueof{/pgfplots/xmin},0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);
\addplot+[ultra thick, mark=none,
    pattern={Lines[angle=45,distance={5pt}]},
    pattern color=red,draw=red]
    fill between [of=A and B,soft clip={domain=-0.5:0.25}];
    \draw[stealth-,thick] (0.25,0.1) -- ++ (axis direction cs:0.5,0)
    node[above]{$\delta x$};
    \draw[stealth-,thick] (-0.5,0.1) -- ++ (axis direction cs:-0.5,0);
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容