我已經能夠執行下面的程式碼,但它仍然不是我需要的。
我需要點 a1 到 a5 形成一條到點「Gol」的直線。
我怎麼做?
.dat 檔案如下所示 (inimigo.dat):
52 10 a0
3 41 a1
51 52 a2
22 62 a3
36 32 a4
pontofixo.dat
115 45 Goal
最佳數據
66.6357 27.6357 g0
48.2417 38.2417 g1
53.5413 63.5413 g2
57.8469 18.8469 g3
75.6483 40.2518 g4
首字母.dat
65 26 i0
47 37 i1
6 16 i2
44 5 i3
58 6 i4
利用這些座標,如何從點 a0 到點「Gol」畫一條線?
\documentclass[varwidth]{standalone}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{geometry}
\geometry{
paperwidth=25cm,
left=1in,right=1in,top=1in,bottom=1in
}
\begin{document}
\begin{figure}[h]
\centering
\begin{subfigure}{.4\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=120,ymin=0,ymax=90, xstep=1,ystep=1,nodes near coords,enlargelimits=0.0]
\addplot +[only marks,mark=*,nodes near coords={\labelz}, visualization depends on={value \thisrowno{2}\as\labelz}]
table[header=false]{pontofixo.dat};
\addplot +[only marks,mark=*,nodes near coords={\labelz}, visualization depends on={value \thisrowno{2}\as\labelz}]
table[header=false]{inimigo.dat};
\addplot +[only marks,mark=*,nodes near coords={\labelz},visualization depends on={value \thisrowno{2}\as\labelz}]
table[header=false]{inicial.dat};
\end{axis}
\end{tikzpicture}
\caption{Estado inicial}
\end{subfigure}%
\centering
\begin{subfigure}{.4\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=120,ymin=0,ymax=90, xstep=1,ystep=1,nodes near coords,enlargelimits=0.0]
\addplot +[only marks,mark=*,nodes near coords={\labelz}, visualization depends on={value \thisrowno{2}\as\labelz}]
table[header=false]{pontofixo.dat};
\addplot +[only marks,mark=*,nodes near coords={\labelz}, visualization depends on={value \thisrowno{2}\as\labelz}]
table[header=false]{inimigo.dat};
\addplot +[only marks,mark=*,nodes near coords={\labelz}, visualization depends on={value \thisrowno{2}\as\labelz}]
table[header=false]{gbest.dat};
\end{axis}
\end{tikzpicture}
\caption{Estado final}
\end{subfigure}%
\end{figure}
\end{document}
答案1
完整修訂: 回答這個問題一段時間後,我發現這個很酷的技巧,這允許人們以更優雅的方式實現目標。在我想起我的原始程式碼後這個問題,我覺得我需要更新這個答案
\documentclass[varwidth]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{inimigo.dat}
x y label
52 10 a0
3 41 a1
51 52 a2
22 62 a3
36 32 a4
\end{filecontents*}
\begin{filecontents*}{pontofixo.dat}
x y label
115 45 Goal
\end{filecontents*}
\begin{filecontents*}{inicial.dat}
x y label
65 26 i0
47 37 i1
6 16 i2
44 5 i3
58 6 i4
\end{filecontents*}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{geometry}
\geometry{
paperwidth=25cm,
left=1in,right=1in,top=1in,bottom=1in
}
\pgfplotsset{% https://tex.stackexchange.com/a/75811/121799
name nodes near coords/.style={
every node near coord/.append style={
name=#1-\coordindex,
alias=#1-last,
},
},
name nodes near coords/.default=coordnode
}
\begin{document}
\begin{figure}[h]
\centering
\begin{subfigure}{.4\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=120,ymin=0,ymax=90, xstep=1,ystep=1,nodes near coords,enlargelimits=0.0]
\addplot +[only marks,mark=*,nodes near
coords={\labelz},
visualization depends on={value \thisrowno{2}\as\labelz},
name nodes near coords=Gol]
table{pontofixo.dat}; % this defines the coordinate (Goal)
% if I do not define it, the next sequence will throw an error
\addplot +[scatter/position=relative,only marks,mark=*,
nodes near coords={\labelz},
visualization depends on={value \thisrowno{2}\as\labelz},
name nodes near coords=a]
table{inimigo.dat};
\addplot +[scatter/position=relative,only marks,mark=*,
nodes near coords={\labelz},
visualization depends on={value \thisrowno{2}\as\labelz},
name nodes near coords=i]
table{inicial.dat};
\end{axis}
\foreach \n in {0,...,4}
{
\draw[black,thick,dashed] (a-\n.south) -- (Gol-0.south);
\draw[black,thick,dashed] (i-\n.south) -- (Gol-0.south);
}
\end{tikzpicture}
\caption{Estado inicial}
\end{subfigure}%
\centering
\begin{subfigure}{.4\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=120,ymin=0,ymax=90, xstep=1,ystep=1,nodes near coords,enlargelimits=0.0]
\addplot +[only marks,mark=*,nodes near
coords={\labelz},
visualization depends on={value \thisrowno{2}\as\labelz},
name nodes near coords=Gol]
table{pontofixo.dat}; % this defines the coordinate (Goal)
% if I do not define it, the next sequence will throw an error
\addplot +[scatter/position=relative,only marks,mark=*,
nodes near coords={\labelz},
visualization depends on={value \thisrowno{2}\as\labelz},
name nodes near coords=a]
table{inimigo.dat};
\addplot +[scatter/position=relative,only marks,mark=*,
nodes near coords={\labelz},
visualization depends on={value \thisrowno{2}\as\labelz},
name nodes near coords=g]
table{gbest.dat};
\end{axis}
\foreach \n in {0,...,4}
{
\draw[black,thick,dashed] (a-\n.south) -- (Gol-0.south);
\draw[black,thick,dashed] (g-\n.south) -- (Gol-0.south);
}
\end{tikzpicture}
\caption{Estado final}
\end{subfigure}%
\end{figure}
\end{document}
老的: 這是我原來的解決方案。
\documentclass[varwidth]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{inimigo.dat}
x y label
52 10 a0
3 41 a1
51 52 a2
22 62 a3
36 32 a4
\end{filecontents*}
\begin{filecontents*}{pontofixo.dat}
x y label
115 45 Goal
\end{filecontents*}
\begin{filecontents*}{inicial.dat}
x y label
65 26 i0
47 37 i1
6 16 i2
44 5 i3
58 6 i4
\end{filecontents*}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{geometry}
\geometry{
paperwidth=25cm,
left=1in,right=1in,top=1in,bottom=1in
}
\begin{document}
\begin{figure}[h]
\centering
\begin{subfigure}{.4\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=120,ymin=0,ymax=90, xstep=1,ystep=1,nodes near coords,enlargelimits=0.0]
\xdef\DoLater{}
\addplot +[only marks,mark=*,nodes near
coords={\makebox[0pt]{\coordinate(\labelz) at (\myx,\myy);}\labelz},
visualization depends on={value \thisrowno{2}\as\labelz},
visualization depends on={value \thisrow{x}\as\myx},
visualization depends on={value \thisrow{y}\as\myy}]
table{pontofixo.dat}; % this defines the coordinate (Goal)
% if I do not define it, the next sequence will throw an error
\addplot +[scatter/position=relative,only marks,mark=*,
nodes near coords={\labelz\makebox[0pt]{\coordinate(\labelz) at
(\myx,\myy);
\xdef\DoLater{\DoLater,\labelz}
}}, visualization depends on={value \thisrowno{2}\as\labelz},
visualization depends on={value \thisrow{x}\as\myx},
visualization depends on={value \thisrow{y}\as\myy}]
table{inimigo.dat};
\addplot +[scatter/position=relative,only marks,mark=*,
nodes near coords={\labelz\makebox[0pt]{\coordinate(\labelz) at
(\myx,\myy);
\xdef\DoLater{\DoLater,\labelz}
}}, visualization depends on={value \thisrowno{2}\as\labelz},
visualization depends on={value \thisrow{x}\as\myx},
visualization depends on={value \thisrow{y}\as\myy}]
table{inicial.dat};
\end{axis}
\foreach \Point in \DoLater{
\ifx\Point\empty%
\relax
\else
\draw (\Point) -- (Goal);
\fi
}
\end{tikzpicture}
\caption{Estado inicial}
\end{subfigure}%
\centering
\begin{subfigure}{.4\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=120,ymin=0,ymax=90, xstep=1,ystep=1,nodes near coords,enlargelimits=0.0]
\xdef\DoLater{}
\addplot +[only marks,mark=*,nodes near
coords={\makebox[0pt]{\coordinate(\labelz) at (\myx,\myy);}\labelz},
visualization depends on={value \thisrowno{2}\as\labelz},
visualization depends on={value \thisrow{x}\as\myx},
visualization depends on={value \thisrow{y}\as\myy}]
table{pontofixo.dat}; % this defines the coordinate (Goal)
% if I do not define it, the next sequence will throw an error
\addplot +[scatter/position=relative,only marks,mark=*,
nodes near coords={\labelz\makebox[0pt]{\coordinate(\labelz) at
(\myx,\myy);
\xdef\DoLater{\DoLater,\labelz}
}}, visualization depends on={value \thisrowno{2}\as\labelz},
visualization depends on={value \thisrow{x}\as\myx},
visualization depends on={value \thisrow{y}\as\myy}]
table{inimigo.dat};
\addplot +[scatter/position=relative,only marks,mark=*,
nodes near coords={\labelz\makebox[0pt]{\coordinate(\labelz) at
(\myx,\myy);
\xdef\DoLater{\DoLater,\labelz}
}}, visualization depends on={value \thisrowno{2}\as\labelz},
visualization depends on={value \thisrow{x}\as\myx},
visualization depends on={value \thisrow{y}\as\myy}]
table{gbest.dat};
\end{axis}
\foreach \Point in \DoLater{
\ifx\Point\empty%
\relax
\else
\draw (\Point) -- (Goal);
\fi
}
\end{tikzpicture}
\caption{Estado final}
\end{subfigure}%
\end{figure}
\end{document}
說明:在解析表時,定義了具有標籤 ... has a Guess ... 的座標label
,並且座標也儲存在 list 中\DoLater
。此列表已解析外部環境axis
(因為延遲擴展問題),然後繪製連接。 (請注意,此版本依賴坐標標籤的唯一性,但\coordindex
在標籤中添加類似內容以使標籤在未來應用此技巧時變得唯一是很簡單的。)
答案2
這個解決方案的主要想法是給出nodes near coords
a name
,以便稍後可以使用/引用它們來繪製連接線。完成後只需要知道
- 繪製了多少個
\addplot
s 以及 - 每個 中有多少個座標
\addplot
。
有了這些知識,就很容易劃出「目標」的界線。
(我只提供第一張圖的解決方案,然後可以輕鬆地將其應用於第二張圖。)
詳細內容請看程式碼中的註解。
% used PGFPlots v1.15
\begin{filecontents*}{pontofixo.dat}
x y label
115 45 Goal
\end{filecontents*}
\begin{filecontents*}{inimigo.dat}
x y label
52 10 a0
3 41 a1
51 52 a2
22 62 a3
36 32 a4
\end{filecontents*}
\begin{filecontents*}{inicial.dat}
x y label
65 26 i0
47 37 i1
6 16 i2
44 5 i3
58 6 i4
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,
xmax=120,
ymin=0,
ymax=90,
enlargelimits=false,
% moved common options here
only marks,
nodes near coords={\labelz},
% give any "node near coord" a name
nodes near coords style={
name=a\plotnum-\coordindex,
},
visualization depends on={
value \thisrowno{2}\as\labelz
},
% create a cycle list so there is no need for `\addplot' options
cycle multiindex* list={
color\nextlist
mark=*\nextlist
},
]
\addplot table {pontofixo.dat};
\addplot table {inimigo.dat};
\addplot table {inicial.dat};
% store the number of plots which will be needed outside the
% `axis' environment
\pgfmathtruncatemacro{\NumPlots}{\numplots}
\end{axis}
% now draw the lines
\foreach \tab [count=\plotnumber from 1] in {
inimigo.dat,
inicial.dat%
} {
% get the number of rows per table so we know how many lines need to
% be drawn per table
\pgfplotstablegetrowsof{\tab}
\pgfmathtruncatemacro{\NoOfRows}{\pgfplotsretval-1}
\foreach \i in {0,...,\NoOfRows} {
\draw (a\plotnumber-\i.south) -- (a0-0.south);
}
}
\end{tikzpicture}
\end{document}