我有一个像附图这样的图形,它由下面的代码生成。我想画一个虚线边界围绕一些节点,如图所示。最好的方法是什么?任何建议都非常感谢。谢谢。
\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\pgfplotsset{every axis legend/.append style={
at={(0.23,0.74)},
anchor=south}}
\begin{axis}[ xlabel = Field X Axis, ylabel = Field Y Axis , xtick ={0, 50, 100}, ytick ={0, 50, 100}, legend entries ={Node, MC's Position, Base Station}]
\addplot[ gray, only marks] coordinates {(10,15) (10, 60) (2,55) (17, 16) (17,25) (1, 21) (5,45) (83, 10) (56,35) (25, 40)};
\addplot[orange, only marks, mark=square*, mark size=4] coordinates {(0,60) (60,80) (85,15)};
\addplot[cyan, only marks, mark=triangle*, mark size=7] coordinates {(0,5)};
\addplot[no markers, dashed, cyan] coordinates {(0,5) (0, 60)};
\addplot[no markers, dashed, cyan] coordinates {(0,5)(85,15)};
\addplot[no markers, dashed, cyan] coordinates {(0,5) (60,80)};
\end{axis}
\end{tikzpicture}
\caption{System Overview of WRSN}
\end{figure}
\end{document}
答案1
一种可能的方法是命名坐标,您可以在其周围绘制虚线,并选择适合所选坐标的节点形状。为此,fit
必须在文档序言中添加 TikZ 库:
\documentclass{article}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{width=11cm,compat=1.13} % <--- added
\usetikzlibrary{fit,shapes.geometric}% <--- added
\usepackage{amsmath}
\usepackage{graphicx}
% for show figure only
\usepackage[active,floats,tightpage]{preview}
\setlength\PreviewBorder{1em}
\begin{document}
\begin{figure}[h]
\begin{tikzpicture}
\pgfplotsset{every axis legend/.append style={
at={(0.23,0.74)},
anchor=south}}
\begin{axis}[ xlabel = Field X Axis, ylabel = Field Y Axis , xtick ={0, 50, 100}, ytick ={0, 50, 100}, legend entries ={Node, MC's Position, Base Station}]
\addplot[ gray, only marks] coordinates {(10,15) (10, 60) (2,55) (17, 16) (17,25) (1, 21) (5,45) (83, 10) (56,35) (25, 40)};
\addplot[orange, only marks, mark=square*, mark size=4] coordinates {(0,60) (60,80) (85,15)};
\addplot[cyan, only marks, mark=triangle*, mark size=7] coordinates {(0,5)};
\addplot[no markers, dashed, cyan] coordinates {(0,5) (0, 60)};
\addplot[no markers, dashed, cyan] coordinates {(0,5)(85,15)};
\addplot[no markers, dashed, cyan] coordinates {(0,5) (60,80)};
%%%% added
\coordinate (a) at (1, 21);
\coordinate (b) at (10,15);
\coordinate (c) at (17,16);
\coordinate (d) at (17,25);
\node[ellipse, draw, thick, dotted,
fit=(a) (b) (c) (d)] {};
%%%%
\end{axis}
\end{tikzpicture}
\caption{System Overview of WRSN}
\end{figure}
\end{document}
附录:无需命名坐标即可获得相同的结果:
%%%% added
\node[ellipse, draw, thick, dotted,
fit={(1, 21) (10,15) (17,16) (17,25)}] {};% <-- field of coordinates are inside { }
%%%%