我希望两个函数之间的空间有颜色,x 中的域为 0 到 4。但空间填充不起作用。这是我的代码
\documentclass{minimal}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=newest}
\usetikzlibrary{positioning,arrows.meta}
\usetikzlibrary{decorations.markings,bending}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
scale = 0.8,
xmin = 0, xmax = 5,
ymin = 0, ymax = 50,
axis lines = left,
xtick = {0,4}, ytick = {48},
clip = false,
]
\addplot[domain = 0:5, restrict y to domain = 0:50, samples =
400, color = red, name path = upaxis, postaction = {decorate}, decoration = {
markings,
mark = at position 0.4 with {\arrow[black,line width=1pt,xscale=-1 {stealth}}
}]{12*(x)};
\addplot[domain = 0:5, restrict y to domain = 0:50, samples =
400, color = black, name path = downaxis, postaction = {decorate}, decoration = {
markings,
mark = at position 0.6 with {\arrow[line width=1pt]{stealth}}
}]{(3*(x)^2)};
\addplot [blue, draw = none, fill opacity = 0.1] fill between [of
= upaxis and downaxis, soft clip={domain=0:4}];
\addplot[color = gray, dashed, ultra thin] coordinates {(4,0) (4,
48) (0, 48)};
\addplot[color = black, mark = *, only marks, mark size = 0.6pt]
coordinates {(4, 48)};
\node [right] at (current axis.right of origin) {$x$};
\node [above] at (current axis.above origin) {$y$};
\node [above left, draw, align = left] at (1.8, 31) {
\textcolor{red}{$y=12x$} \\
$y=3x^2$
};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
\documentclass{minimal}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=newest}
\usetikzlibrary{positioning,arrows.meta}
\usetikzlibrary{decorations.markings,bending}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
scale = 0.8,
xmin = 0, xmax = 5,
ymin = 0, ymax = 50,
axis lines = left,
xtick = {0,4}, ytick = {48},
clip = false,
]
\addplot[domain = 0:5, restrict y to domain = 0:50, samples =
400, color = red,
postaction = {name path = upaxis,},
postaction = {decorate}, decoration = {
markings,
mark = at position 0.4 with {\arrow[black,line width=1pt,xscale=-1]{stealth}}
}]{12*(x)};
\addplot[domain = 0:5, restrict y to domain = 0:50, samples =
400, color = black,
postaction = {name path = downaxis,},
postaction = {decorate}, decoration = {
markings,
mark = at position 0.6 with {\arrow[line width=1pt]{stealth}}
}]{(3*(x)^2)};
\addplot [blue, draw = none, fill opacity = 0.1] fill between [of
= upaxis and downaxis, soft clip={domain=0:4}];
\addplot[color = gray, dashed, ultra thin] coordinates {(4,0) (4,
48) (0, 48)};
\addplot[color = black, mark = *, only marks, mark size = 0.6pt]
coordinates {(4, 48)};
\node [right] at (current axis.right of origin) {$x$};
\node [above] at (current axis.above origin) {$y$};
\node [above left, draw, align = left] at (1.8, 31) {
\textcolor{red}{$y=12x$} \\
$y=3x^2$
};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
请尝试以下操作:
\documentclass[border=3.14159]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{arrows.meta,
decorations.markings,
intersections}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[
-|-/.style = {preaction=decorate,
decoration={markings,
mark=at position .45 with {#1{Straight Barb}}}
},
dot/.style = {circle, fill=black, inner sep=1.2pt}
]
\begin{axis}[
axis lines = center,
xlabel=$x$,
xtick = {0,4},
ylabel=$y$,
ytick = {48},
label style = {anchor=north east},
enlargelimits = {0.1,upper},
%
legend style = {legend pos = north west, yshift=-3em},
%
restrict y to domain=0:52,
domain = 0:5, samples=51, no marks,
every axis plot post/.append style={line width=1pt},
clip=false
]
\addplot +[name path=A, -|- = \arrow]{12*(x)};
\addplot +[name path=B, -|- = \arrowreversed]{(3*(x)^2)};
\addplot [blue, fill opacity = 0.1]
fill between [of = A and B];
\coordinate (origin) at (0,0);
\draw [name intersections={of=A and B, by={x1,x2}},
very thin, densely dashed]
node[dot] at (x2) {}
(origin |- x2) -| (origin -| x2);
\legend{$y=12x$, $y=3x^2$};
\end{axis}
\end{tikzpicture}
\end{document}
编辑: 可以看到,装饰路径绘制为 \verb+preaction=decorate,+:
答案3
没有箭头也可以正常工作decoration
- 会尝试找出原因吗?
\documentclass{minimal}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=newest}
\usetikzlibrary{positioning,arrows.meta}
\usetikzlibrary{decorations.markings,bending}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
scale = 0.8,
xmin = 0, xmax = 5,
ymin = 0, ymax = 50,
axis lines = left,
xtick = {0,4}, ytick = {48},
clip = false,
]
\addplot[domain = 0:5,
restrict y to domain = 0:50,
samples = 400,
color = red,
name path = upaxis,
% postaction = {decorate},
% decoration = {
% markings,
% mark = at position 0.4 with {\arrow[black,line width=1pt,xscale=-1 {stealth}}
% }
]{12*(x)};
\addplot[domain = 0:5,
restrict y to domain = 0:50,
samples = 400,
color = black,
name path = downaxis,
% postaction = {decorate}, decoration = {
% markings,
% mark = at position 0.6 with {\arrow[line width=1pt]{stealth}}
% }
]{(3*(x)^2)};
\addplot [blue,] fill between [
of = upaxis and downaxis,
soft clip={domain=0:4}
];
\addplot[color = gray, dashed, ultra thin] coordinates {(4,0) (4,
48) (0, 48)};
\addplot[color = black, mark = *, only marks, mark size = 0.6pt]
coordinates {(4, 48)};
\node [right] at (current axis.right of origin) {$x$};
\node [above] at (current axis.above origin) {$y$};
\node [above left, draw, align = left] at (1.8, 31) {
\textcolor{red}{$y=12x$} \\
$y=3x^2$
};
\end{axis}
\end{tikzpicture}
\end{document}