![在 pgfplot 中标记小宽度](https://linux22.com/image/411979/%E5%9C%A8%20pgfplot%20%E4%B8%AD%E6%A0%87%E8%AE%B0%E5%B0%8F%E5%AE%BD%E5%BA%A6.png)
我只是想问一下如何用箭头标记条带的宽度并像 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}
答案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}