我想用阴影图案表示不等式约束。这将在 中完成pgfplot
,它本身是beamer
框架的一部分。
以下是 MWE:
\documentclass{beamer}
\usepackage{tikz,pgfplots}
\usetikzlibrary{patterns}
\begin{document}
\begin{frame}{Test}
\begin{tikzpicture}[remember picture]
\begin{axis}[
width=\textwidth,
minor tick num=1,
axis y line=center,
axis x line=middle,
]
\addplot[smooth, cyan, mark=none, domain=-3:3, samples=40, thick] {x^2};
\draw[red, thick] (axis cs:-2, 0) -- (axis cs:-2, 9);
\draw[pattern=north west lines, thin, red] (axis cs:-2,0)
rectangle (axis cs:-1.75, 9);
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
但最终的输出是:
如您所见,画的是实线,而不是图案。我该如何修复这个问题?
答案1
这与 的使用无关beamer
。要指定图案的颜色,您必须使用draw=red, pattern color=red
而不是red
:
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=\textwidth,
minor tick num=1,
axis y line=center,
axis x line=middle,
]
\addplot[smooth, cyan, mark=none, domain=-3:3, samples=40, thick] {x^2};
\draw[pattern=north west lines, draw=red, pattern color=red] (axis cs:-2,0)
rectangle (axis cs:-1.75, 9);
\end{axis}
\end{tikzpicture}
\end{document}
答案2
使用pattern color=red
应该可以解决问题
代码
\documentclass{beamer}
\usepackage{tikz,pgfplots}
\usetikzlibrary{patterns}
\begin{document}
\begin{frame}{Test}
\begin{tikzpicture}[remember picture]
\begin{axis}[
width=\textwidth,
minor tick num=1,
axis y line=center,
axis x line=middle,
]
\addplot[smooth, cyan, mark=none, domain=-3:3, samples=40, thick] {x^2};
\draw[red, thick] (axis cs:-2, 0) -- (axis cs:-2, 9);
\draw[pattern=north west lines, thin, pattern color=red] (axis cs:-2,0)
rectangle (axis cs:-1.75, 9);
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
答案3
PSTricks 中最简单的解决方案只是为了好玩。
\documentclass{beamer}
\usepackage{pst-plot}
\begin{document}
\begin{frame}{Test}
\psgraph(0,0)(-3,0)(3,9){5cm}{6cm}
\psplot[algebraic,linecolor=blue]{-3}{3}{x^2}
\psframe[dimen=m,fillstyle=vlines,hatchcolor=red](-2,0)(-1.5,9)
\endpsgraph
\end{frame}
\end{document}