我已经能够執行下面的代碼,但它仍然不是我需要的。
我需要點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
gbest.data
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
inicial.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}
获得此結果:
但我需要類似的(在inskcape中編輯):
最新回復
- 2019-12-51 #
- 2019-12-52 #
這个解決方案的主要思想是给
nodes near coords
一个name
因此可以在以後使用/引用它们来绘製連線線.完成後,只需要知道how many
\addplot
在哪裏绘製和每个
\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}
相似問題
- LaTeX tikz pgf:為什麼绘圖不完整?LaTeXtikzpgfLaTeXpgfplots2019-12-05 21:44
- LaTeX tikz pgf:使用tikzpicture和pgfplots調整向量LaTeXtikzpgfLaTeXpgfplots2019-12-05 21:44
- LaTeX pgfplots:使用Tikz覆盖两个褪色圆圈LaTeXtikzpgfLaTeXpgfplotsLaTeXgraphics2019-12-05 21:40
- 暂時禁用pgfplots中的Tikz阴影LaTeXtikzpgfLaTeXpgfplots2019-12-05 21:40
- LaTeX tikz pgf:3D立方體,虛線和面填充LaTeXtikzpgfLaTeXpgfplots2019-12-05 21:41
COMPLETE REVISION :在我迴答這个問题後的某个時候,我遇到了這个很酷的伎俩,它允许人们以更優雅的方式實現目標.在我提醒我在這个問题中的原始代碼之後,我覺得我需要將此答案更新為
OLD :這是我原来的解決方案。
說明:在解析表格時,会定義具有標簽的坐標......猜測...
label
,坐標也儲存在列表\DoLater
中 .此列表在外部解析axis
環境(由於延迟擴充套件問题)然後绘製連線. (請註意,此版本依赖於坐標標簽是唯一的,但添加像\coordindex
這樣的东西很簡單 在標簽上使標簽在這个技巧的未来應用中独一無二。)