问题
我想pgfplot
通过在画布上手动绘制路径来指示我的某个 s 中的不可行区域。这应该是装饰在路径上的阴影图案,类似于 north east lines
from \usepgflibrary{patterns}
。
平均能量损失
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% axis equal image,
xlabel = {$x$},
ylabel = {$y$},
grid,
xmin = 0, xmax = 3000,
ymin=400, xmax = 2200,
width=0.5\textwidth,
every node near coord/.append style={font=\tiny\scshape},
nodes near coords align={vertical},
]
\addplot+[only marks, nodes near coords, point meta=explicit symbolic]
table[meta=label] {
x y label
2500 1500 a
1306 1655 b
200 1000 c
1600 1570 d
1500 2000 e
};
% path to be decorated
\draw (axis cs:1000, 500) -- (axis cs:2750,500) -- (axis cs:2750, 2000)
-- (axis cs: 1000, 2000) -- cycle;
\end{axis}
\end{tikzpicture}
\end{document}
挑战
不幸的是我不知道该如何解决这个问题。你能帮忙吗?
以下是一些挑战:
也许这可以通过装饰路径来实现,但我不知道这是如何工作的。
重要的是,模式必须以实际路径为界,并“向外凸出”。模式不得替代路径,而必须“在边缘”。奖励:为了指示逆问题,模式也可以“向内凸出”。
north east lines
有时看起来比 更好north west lines
,取决于方向。这个选择可以自动化吗?
插图
我所说的“向外凸出”是指:
“向内” 则是相反的:
我为这张亵渎神灵的画作道歉。因为它看起来很糟糕,所以我们需要 Tikz!
答案1
另一个可能的解决方案是使用 tikzlibrary 的clip
技术和patterns
方法。
附录:
为了使它们自动化,可以定义一个带有两个参数的宏,例如 #1=线条图案,#2=颜色。同样的想法也适用于inwards
宏。
\newcommand\outwards[2]{
\begin{scope}
\draw (axis cs:1000, 500) -- (axis cs:2750,500) -- (axis cs:2750, 2000) -- (axis cs: 1000, 2000) -- cycle;
\clip (axis cs:900, 450) rectangle (axis cs:2850, 2050);
\path[pattern=#1, pattern color=#2,] (axis cs:900, 450) rectangle (axis cs:2850, 2050);
\fill[white] (axis cs:1000, 500) rectangle (axis cs:2750, 2000);
\end{scope}
}
然后打电话
\outwards{north west lines}{red}
代码
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% axis equal image,
xlabel = {$x$},
ylabel = {$y$},
grid,
xmin = 0, xmax = 3000,
ymin=400, xmax = 2200,
width=0.5\textwidth,
every node near coord/.append style = {font=\tiny\scshape},
nodes near coords align={vertical},
enlarge x limits={rel=0.5, upper}
]
% --- code for bulging outwards
\begin{scope}
\draw (axis cs:1000, 500) -- (axis cs:2750,500) -- (axis cs:2750, 2000) -- (axis cs: 1000, 2000) -- cycle;
\clip (axis cs:900, 450) rectangle (axis cs:2850, 2050);
\path[pattern=north west lines, pattern color=red,] (axis cs:900, 450) rectangle (axis cs:2850, 2050);
\fill[white] (axis cs:1000, 500) rectangle (axis cs:2750, 2000);
\end{scope}
% --- code for bulging inwards
\begin{scope}
\clip (axis cs:1000, 500) rectangle (axis cs:2750, 2000);
\path[pattern=north east lines,pattern color=green,] (axis cs:1000, 500) rectangle (axis cs:2750, 2000);
\fill[white] (axis cs:1100, 560) rectangle (axis cs:2650, 1940);
\end{scope}
\addplot+[only marks, nodes near coords, point meta=explicit symbolic]
table[meta=label] {
x y label
2500 1500 a
1306 1655 b
200 1000 c
1600 1570 d
1500 2000 e
};
% path to be decorated
\draw (axis cs:1000, 500) -- (axis cs:2750,500) -- (axis cs:2750, 2000) -- (axis cs: 1000, 2000) -- cycle;
\draw[gray!50!white] (axis cs:1000, 1000) -- (axis cs:2750, 1000);
\draw[gray!50!white] (axis cs:1000, 1500) -- (axis cs:2750, 1500);
\draw[gray!50!white] (axis cs:2000, 500) -- (axis cs:2000, 2000);
\end{axis}
\end{tikzpicture}
\end{document}
答案2
使用 TikZ 装饰来实现这一点并不难:
\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% axis equal image,
xlabel = {$x$},
ylabel = {$y$},
grid,
xmin = 0, xmax = 3000,
ymin=400, ymax = 2200,
width=0.5\textwidth,
every node near coord/.append style={font=\tiny\scshape},
nodes near coords align={vertical},
]
\addplot+[only marks, nodes near coords, point meta=explicit symbolic]
table[meta=label] {
x y label
2500 1500 a
1306 1655 b
200 1000 c
1600 1570 d
1500 2000 e
};
% path to be decorated
\draw[
decoration={border,segment length=1mm,amplitude=5mm,angle=-135},
postaction={decorate,draw}
] (axis cs:1000, 500) -- (axis cs:2750,500) -- (axis cs:2750, 2000)
-- (axis cs: 1000, 2000) -- cycle;
\end{axis}
\end{tikzpicture}
\end{document}
对于逆问题使用正数angle
,因此线指向内部。