我教大学代数,需要制作黑白讲义,展示不等式系统的解。通常,我会给每个不等式的解侧涂上颜色,并注意所有不等式的答案是所有颜色重叠的区域。在黑白讲义中,这通常是通过对解侧进行哈希处理来实现的。但是,当你向系统中添加不等式时,这可能会变得一团糟,难以阅读(例如,参见如何绘制不平等区域?, 或者如何绘制不平等区域)。我想要做的是进行哈希处理,如下面的示例图所示(不是来自我的 MWE),其中哈希只是遵循不等式的边缘,而不是填充整个区域。
这是使用 pgfplots 绘制两个不等式的 MWE。如何沿边缘进行哈希处理?请注意,我需要的解决方案不仅适用于线性不等式,还适用于抛物线和圆的图形。
\documentclass[10pt]{article}
\usepackage{pgfplots}
\begin{document}
$\left\{ \begin{array}{l}
y \le -3x + 6 \\
x > -2 \\
y \le 4 \\
\end{array} \right.$
\begin{tikzpicture}
\begin{axis}[
axis x line=center, axis y line=center,
width=4in, height=4in,
x axis line style={<->},
y axis line style={<->},
xmin=-10, xmax=10,
ymin=-10, ymax=10
]
\addplot [very thick,cyan!50!black,domain=-10:10] {6 - 3*x};
\addplot [very thick,cyan!50!black,domain=-10:10] {4};
\addplot [very thick,cyan!50!black,dashed] coordinates {(-2,-10) (-2,10)};
\end{axis}
\end{tikzpicture}
\end{document}
[更新]
下面提供了这个问题的一个很好的答案。我还偶然发现了在 TikZ 中使用填充图案装饰路径的边缘由此我也能开发下面的代码。为了完整起见,我在这里发布了一个示例输出图像。
\documentclass[10pt]{article}
\usepackage{pgfplots}
\begin{document}
$\left\{ \begin{array}{l}
y \le -3x + 6 \\
x > -2 \\
y \le 4 \\
y \ge x^2-4 \\
\end{array} \right.$
% Amplitude is the length of the hatching.
% Segment Length is the distance between the marks.
% Angle is the orientation of the hatches with +-90 being perpendicular.
\pgfplotsset{IneqStyleLess/.style = {%
decoration={border,segment length=1mm, amplitude=3mm,angle=-90},
postaction={decorate,draw},
thick,blue!50!white, draw opacity=0.5
}
}
\pgfplotsset{IneqStyleGtr/.style = {%
decoration={border,segment length=1mm, amplitude=3mm,angle=90},
postaction={decorate,draw},
thick,blue!50!white, draw opacity=0.5
}
}
\begin{tikzpicture}
\begin{axis}[
axis x line=center, axis y line=center,
axis on top,
width=4in, height=4in,
x axis line style={<->},
y axis line style={<->},
xmin=-10, xmax=10,
ymin=-10, ymax=10
]
\addplot [IneqStyleLess,
domain=-10:10,
] {6 - 3*x};
\addplot [IneqStyleLess,
thick,red!50!white,
domain=-10:10,
] {4};
\addplot [IneqStyleLess,
thick,green!50!white] coordinates {(-2,-10) (-2,10)};
\addplot [IneqStyleGtr,
thick,purple!50!white,
domain=-10:10,
samples=100,
] {x*x-4};
\addplot [black,domain=-10:10] {6 - 3*x};
\addplot [black,domain=-10:10] {4};
\addplot [black,dashed] coordinates {(-2,-10) (-2,10)};
\addplot [black,domain=-10:10, samples=100] {x*x-4};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您可以使用decoration
类似PGFplots:装饰情节,但不装饰图例,但在绘制线条时,必须使用axis direction cs
才能使其工作。 这样做的缺点是线条的长度以轴单位指定,因此必须根据轴限制缩放线条。
\documentclass[10pt]{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\usetikzlibrary{decorations.markings}
\tikzset{
region/.style={
postaction={decorate},
decoration={
markings,
mark=between positions 0 and 1 step 5mm with {%
% note that the values are in axis units
\draw [solid,thin] (axis direction cs:0,-0.1*#1) -- (axis direction cs:0,-1*#1);
}
},
/pgfplots/forget plot
},
region/.default=1
}
\begin{document}
$\begin{cases}
y \le -3x + 6 \\
x > -2 \\
y \le 4 \\
\end{cases}$
\begin{tikzpicture}
\begin{axis}[
axis x line=center, axis y line=center,
width=4in, height=4in,
x axis line style={<->},
y axis line style={<->},
xmin=-10, xmax=10,
ymin=-10, ymax=10,
every axis plot/.append style={
very thick,
cyan!50!black,
domain=-10:10,
samples=2, % all you need for straight lines
}
]
\addplot [region] {6 - 3*x};
\addplot [region] {4};
\addplot [region,dashed] coordinates {(-2,-10) (-2,10)};
\end{axis}
\end{tikzpicture}
\end{document}