我目前有这个代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\definecolor{linecolor1}{HTML}{3DC6F3}
\pgfplotsset{integration/.style={color=linecolor1,mark=none,line width=0.5pt,solid}}
\pgfplotsset{every axis/.append style={
axis x line=middle,
axis y line=middle,
ticks=none,
axis line style={->},
xlabel={$x$},
ylabel={$y$},
samples=1000,
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-2,xmax=4,
ymin=-2,ymax=8,
]
\addplot[integration]{-(1/4)*x*(x+2)*(x-1)*(x-4)};
\end{axis}
\end{tikzpicture}
\end{document}
我如何创建integration
样式来为曲线下的区域着色?
答案1
如果要填充曲线下方的所有内容,请添加fill=blue!30
到integration/.style
。如果应填充曲线和 x 轴之间,请参见下文。
改编
变体 1
- 在 0 处添加第二个图(
\addplot [name path=xaxis] {0}
)或- 如果它恰好是刚刚设置的 x 轴
x axis line style={name path=xaxis}
(参见评论赫佩克里斯蒂安森)
- 如果它恰好是刚刚设置的 x 轴
- 给函数命名
name path
(例如curve
) - 添加
\usepgfplotslibrary{fillbetween}
定义fill between
命令:\addplot[fill=blue!30] fill between[of=curve and xaxis];
变体 2
fill=blue!30
直接在图上使用并添加\closedcycle
(这将在 x 轴上画一条线)(参见评论赫佩克里斯蒂安森)
结果
变体 1:
变体 2:
代码
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{fillbetween}
\definecolor{linecolor1}{HTML}{3DC6F3}
\pgfplotsset{
integration/.style={color=linecolor1,mark=none,line width=0.5pt,solid},
every axis/.append style={
axis x line=middle,
axis y line=middle,
ticks=none,
axis line style={->},
xlabel={$x$},
ylabel={$y$},
samples=1000,
x axis line style={name path=xaxis},
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-2,xmax=4,
ymin=-2,ymax=8,
domain=-5:5,
]
% variant 1:
\addplot [name path=curve, integration]{-(1/4)*x*(x+2)*(x-1)*(x-4)};
\addplot [fill=blue!30] fill between[of=curve and xaxis];
%
% variant 2:
%\addplot [name path=curve, integration, fill=blue!30]{-(1/4)*x*(x+2)*(x-1)*(x-4)} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}