我正在尝试绘制一个基本图表,pgfplots
但遇到了一个问题,我无法根据类似的帖子解决。我尝试使用节点将图表上显示的函数添加到各自的线上,但最终得到以下结果:
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{tikzpicture}
\begin{axis}[axis lines=center,
xlabel=$x$,
ylabel=$y$,
xmin=-1,
xmax=1.5,
ymin=-1,
ymax=2.5,
xtick distance=1,
clip=true,]
\addplot[name path=A, thick, black, samples=200]{x} node[pos=0.61, right]{$y=x$};
\addplot[name path=B, thick, black, samples=200]{x + 1} node[pos=0.61, right]{$y=x + 1$};
\addplot[fill=red]fill between[of=A and B, soft clip={domain=0:1}];
\end{axis}
\end{tikzpicture}
从图片中可以看出,y = x _+ 1
被切断了,而y = x
就在边缘,勉强保存下来。我尝试设置,clip=false
但我的图表变得混乱,偏离了中心并散布到整个页面:
有没有办法来解决这个问题?
答案1
只需将域添加到函数中
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{tikzpicture}
\begin{axis}[axis lines=center,
xlabel=$x$,
ylabel=$y$,
xmin=-1,
xmax=2,
ymin=-1,
ymax=2.5,
xtick distance=1]
\addplot[name path=A, thick, black, samples=20, domain=-1:2]{x} node[pos=0.61, right]{$y=x$};
\addplot[name path=B, thick, black, samples=20, domain=-1:2]{x + 1} node[pos=0.61, right]{$y=x + 1$};
\addplot[fill=red]fill between[of=A and B, soft clip={domain=0:1}];
\end{axis}
\end{tikzpicture}