向 pgfplot 添加标签

向 pgfplot 添加标签

如何在 pgfplot 的 x 轴上添加标签。下面是 MWE。我只想在 x 轴上标出标签 -0.7。

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}


\begin{document}

\begin{tikzpicture} 
\begin{axis}[
   width=60mm,
   height=40mm,
   axis lines=left,
   xlabel={$x$},
   every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
   ylabel={$f(x)$},
   every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
   enlargelimits={value=0.1,upper},
   scaled ticks=false, 
   minor x tick num=1,
   ytick=\empty,
   ]
\addplot[blue,domain=-3:3,samples=50]
{exp(-(x)^2 / 2) / (sqrt(2*pi))};

\addplot[blue,fill=blue!50!white, domain=-3:-0.7,samples=50]
{exp(-(x)^2 / 2) / (sqrt(2*pi))}\closedcycle;

\end{axis} 
\end{tikzpicture}


\end{document}

答案1

添加这些

   extra x ticks={-0.7},
   extra x tick style={grid=major,
    tick label style={
    rotate=90,anchor=east}},
   extra x tick labels={$-0.7$},

到轴选项。如果你不想美化

extra x ticks={-0.7},
extra x tick labels={$-0.7$},

就足够了。

代码:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}


\begin{document}

\begin{tikzpicture}
\begin{axis}[
   width=60mm,
   height=40mm,
   axis lines=left,
   xlabel={$x$},
   every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
   ylabel={$f(x)$},
   every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
   enlargelimits={value=0.1,upper},
   scaled ticks=false,
   minor x tick num=1,
   ytick=\empty,
   extra x ticks={-0.7},
   extra x tick style={grid=major,
    tick label style={
    rotate=90,anchor=east}},
   extra x tick labels={$-0.7$},
   ]
\addplot[blue,domain=-3:3,samples=50]
{exp(-(x)² / 2) / (sqrt(2*pi))};

\addplot[blue,fill=blue!50!white, domain=-3:-0.7,samples=50]
{exp(-(x)² / 2) / (sqrt(2*pi))}\closedcycle;

\end{axis}
\end{tikzpicture}


\end{document}

在此处输入图片描述

未经过美化:

在此处输入图片描述

对于投影仪,你可以自己画出来:

\documentclass{beamer}

\usepackage{tikz}
\usepackage{pgfplots}


\begin{document}
\begin{frame}
\begin{tikzpicture}
\begin{axis}[
   width=60mm,
   height=40mm,
   axis lines=left,
   xlabel={$x$},
   every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
   ylabel={$f(x)$},
   every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
   enlargelimits={value=0.1,upper},
   scaled ticks=false,
   minor x tick num=1,
   ytick=\empty,clip=false
   ]
\addplot[blue,domain=-3:3,samples=50]
{exp(-(x)² / 2) / (sqrt(2*pi))};

\only<2->{\addplot[blue,fill=blue!50!white, domain=-3:-0.7,samples=50]
{exp(-(x)² / 2) / (sqrt(2*pi))}\closedcycle;}
\only<2->{\node[text=red,font=\small,rotate=90,overlay] at (axis cs:-0.7,-0.1) {$-0.7$};
}
\only<2->{\draw[thin,red,overlay] (axis cs: -0.7,-0.02) -- (axis cs: -0.7,0.4);
}
\end{axis}
\end{tikzpicture}
\end{frame}

\end{document}

在此处输入图片描述

答案2

不同的标记技能怎么样?pin该技能的使用与命令无关addplotarticle类也一样——删除beamer类和环境和only<2->

在此处输入图片描述 在此处输入图片描述

代码

\documentclass{beamer}

\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{arrows}

\begin{document}
\begin{frame}
\begin{tikzpicture} 
\begin{axis}[
   width=60mm,
   height=40mm,
   axis lines=left,
   xlabel={$x$},
   every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
   ylabel={$f(x)$},
   every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
   enlargelimits={value=0.1,upper},
   scaled ticks=false, 
   minor x tick num=1,
   ytick=\empty,
   ]
\addplot[blue,domain=-3:3,samples=50]
{exp(-(x)^2 / 2) / (sqrt(2*pi))};

\addplot[blue,fill=blue!50!white, domain=-3:-0.7,samples=50]
{exp(-(x)^2 / 2) / (sqrt(2*pi))} \closedcycle;

\only<2->{\node[pin={[pin distance=0.5cm,pin edge={<-,>=stealth'}]above right:$0.7$}] at (axis cs: -0.7,0){};}
\end{axis} 
\end{tikzpicture}
\end{frame}

\end{document}

相关内容