我昨天问了一个问题,用图案填充此抛物线社区已经回答了这个问题,我还有一个问题要问——如何在这个抛物线中构造一个小的彩色/阴影条?类似于:
答案1
使用\clip
如下
\documentclass[tikz, border=3.141592]{standalone}
\usetikzlibrary{patterns.meta}
\begin{document}
\begin{tikzpicture}[
dot/.style = {circle, fill, node contents={},
inner sep=1.6pt, outer sep=0pt}
]
% axis
\draw[->] (-3,0.0) -- (3,0) node [below left] {$x$};
\draw[->] (0,-0.5) -- (0,6) node [below left] {$y$};
% parabola
\draw [thick,
pattern={Lines[angle=45,distance={3pt},line width=0.2pt]},
pattern color=gray]
(-2,5) parabola bend (0,0) (2,5);
\draw[densely dotted, semithick]
(-2,5) node[dot]-- (2,5) node[dot];
% coordinate origin
\path (0,0) node[below left] {(0,0)};
\clip (-2,5) parabola bend (0,0) (2,5);
\draw [red,
pattern={Lines[angle=60,distance={1.5pt},line width=0.2pt]},
pattern color=red] (0.5,0) rectangle (1.5,5);
\end{tikzpicture}
\end{document}
答案2
一种使用 PGFplots 的方法(这不是基于链接的答案,但我认为,使用 PGFplots 绘制这样的图表更容易一些,因此有这种替代解决方案):
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns.meta}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
enlargelimits=0.25,
ticks=none,
xlabel=$x$,
ylabel=$y$,
]
\addplot[no markers, smooth, domain={-4:4}, name path=para, pattern={Lines[angle=45]}] {x^2};
\addplot[mark=*, black, dashed, name path=line] coordinates {(-4,16) (4,16)};
\addplot[red] fill between [of=para and line, soft clip={domain=1:3}];
\end{axis}
\end{tikzpicture}
\end{document}