我正在尝试绘制平面的这个区域
德= {(x,y) ∈ ℝ 2 :X∈[a,b], −x 2 +X≤ y ≤ −X3 +2X; 0 ≤X≤1}
这就是我目前的情况
\documentclass[preview, convert={size=598x414,outext=.png}]{standalone}
\usepackage{pgfplots,tikz}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis} [xlabel = x, ylabel = y, axis lines=center]
\addplot [domain=0:1, smooth] {2*x-x^3};
\addplot [domain=0:1, smooth] {x-x^2};
\draw [thick] (1,0) -- (1,1);
\end{axis}
\end{tikzpicture}
\end{document}
我需要一种方法来为区域 D 着色,同时我想将标签放在x
x 轴下方。
答案1
您只需要对您的代码进行一些细微的调整。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmax=1.1,
axis lines=center,
xlabel=$x$,
xlabel style={at={(axis description cs:1.01,0.0)},anchor=north},
ymax=1.1,
ylabel = $y$
]
\addplot [name path=A,domain=0:1, smooth] {2*x-x^3};
\addplot [name path=B,domain=0:1, smooth] {x-x^2};
\addplot [gray!20] fill between[of=A and B];
\addplot [black] coordinates {(1,0) (1,1)};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
这是@kabenyuk 变体的改进版本,其中我们不需要额外的线来关闭填充区域。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmax=1.1,
axis lines=center,
xlabel=$x$,
xlabel style={at={(axis description cs:1.01,0.0)},anchor=north},
ymax=1.1,
ylabel = $y$
]
\addplot [name path=A,domain=0:1, smooth] {2*x-x^3};
\addplot [name path=B,domain=0:1, smooth] {x-x^2};
\addplot [fill=gray!20,draw=black] fill between[of=A and B];% <-- added draw=black
\end{axis}
\end{tikzpicture}
\end{document}
答案3
使用 MetaPost 来实现这一目的的方法,包含在 LuaLaTeX 程序中,对于可能感兴趣的人,可以使用buildcycle
Plain MetaPost 的宏。
\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
\mplibsetformat{metafun}
\mplibnumbersystem{decimal}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
vardef function(expr xmin, xmax, xstep)(text f_x) =
save x; x := xmin;
(x, f_x)
forever: hide(x := x + xstep) exitif x > xmax;
.. (x, f_x)
endfor
if x - xstep < xmax: hide(x := xmax) .. (x, f_x) fi
enddef;
vardef f(expr x) = x - x**2 enddef;
vardef g(expr x) = 2x - x**3 enddef;
u = v = 8cm; path area; len := 2bp;
area = buildcycle(
function(0, 1, .05)(f(x)),
(1, f(1)) -- (1, g(1)),
function(0, 1, .05)(g(x))
) xyscaled (u,v);
beginfig(1);
fill area withcolor .8white; draw area;
drawarrow origin -- (1.1u, 0); drawarrow origin -- (0,1.1v);
for i = 0.2 step .2 until 1:
draw (i*u, -len) -- (i*u, len);
label.bot("$" & decimal i & "$", (i*u, 0));
draw (-len , i*v) -- (len, i*v);
label.lft("$" & decimal i & "$", (0, i*v));
endfor;
endfig;
\end{mplibcode}
\end{document}