我有一个分段定义的函数,想在它的右端添加一个描述节点。不幸的是,使用以下代码,节点被切断,不可见:
\begin{tikzpicture}
\begin{axis}[
axis x line=center,
axis y line=center,
ymin=-0.2, ymax=1.2,
]
\addplot[blue,domain=-4:1] { (x<1) ? 0 : 1/0 };
\addplot[blue,domain=1:+4] { (x>=1)? 0.5 : 1/0 } node[right] {
$f(x) = \begin{cases}
0 & \mbox{if } x < 1 \\
0.5 & \mbox{if } x \geq 1
\end{cases}$};
\end{axis}
\end{tikzpicture}
如何扩展轴右侧的空间?节点位置应类似于此:
\begin{tikzpicture}
\draw[->] (-1,0) -- (5.5,0) node[right] {$x$};
\draw[->] (0,-3) -- (0,3.5) node[above] {$f(x)$};
\foreach \x in {-1, 0, 1, 2}
\draw (\x cm,1pt) -- (\x cm,-3pt)
node[anchor=north,xshift=-0.15cm] {$\x$};
\foreach \y/\ytext in {-3,-2,-1,1,2,3}
\draw (1pt,\y cm) -- (-3pt,\y cm) node[anchor=east] {$\ytext$};
\draw[color=blue] plot[samples=100] function{log(x)}
node[right] {$f(x) = \log(x) $};
\end{tikzpicture}
答案1
添加clip=false
到轴选项。
\documentclass[12pt,a4paper]{article}
\usepackage{pgfplots,amsmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=center,
axis y line=center,
ymin=-0.2, ymax=1.2,clip=false
]
\addplot[blue,domain=-4:1] { (x<1) ? 0 : 1/0 };
\addplot[blue,domain=1:+4] { (x>=1)? 0.5 : 1/0 } node[right] {
$f(x) = \begin{cases}
0 & \text{if $x < 1$} \\
0.5 & \text{if $x \geq 1$}
\end{cases}$};
\end{axis}
\end{tikzpicture}
\end{document}