我有以下代码,它使我在轴上形成抛物线,我想填充抛物线下方的区域(保持在轴上)。似乎使用 plot 和 patch 应该可以实现,但我没有找到解决方案。你能帮帮我吗?
代码:
\documentclass{amsart}
\usepackage{pgf,tikz}
\usepackage{pgfplots}
\tikzset{My Line Style/.style={smooth, thick, samples=400}}
\begin{document}
\begin{center}\begin{tikzpicture}[scale=0.60]
\begin{axis}[axis lines=middle, xmin=-3.5, xmax = 3.5, ymin=-5.1, ymax = 3,xtick={-3,-2,...,3},
ytick={-5,...,3}]
\addplot[My Line Style, color=lightgray, variable=\t, domain=-3.5:3.5]({\t},{\t^2-0.5});
\end{axis}
\end{tikzpicture}\end{center}\end{document}
答案1
以下是实现此目的的一种可能方法。有关更多详细信息,请查看代码中的注释。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
% use this `compat' level or higher to use `axis cs:' coordinates by
% default for tikz commands
compat=1.11,
My Line Style/.style={
smooth,
thick,
samples=400,
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xmin=-3.5,
xmax=3.5,
ymin=-5.1,
ymax=3,
xtick={-3,-2,...,3},
ytick={-5,...,3},
% use this so the axis is drawn on top of the everything
axis on top,
]
% first draw the fill stuff
\addplot [
My Line Style,
draw=none,
fill=lightgray!25,
domain=-3.5:3.5,
% restrict the y values to the maximum y axis value
restrict y to domain*=-5.1:3,
] {x^2-0.5}
% continue the path to close it
|- (\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin})
;
% and on top of that the line
\addplot [
My Line Style,
draw=lightgray,
domain=-3.5:3.5,
] {x^2-0.5}
;
\end{axis}
\end{tikzpicture}
\end{document}