從外部座標檔案繪製 3D 平滑曲線

從外部座標檔案繪製 3D 平滑曲線

我想根據先前計算的保存在文件中的坐標(例如“xz.dat”)創建 3D 繪圖。

其中所包含的數據如下:

x z
0.5 0
0.54 0.01
0.58 0.01
0.62 0.02
0.66 0.03
0.7 0.03
0.74 0.04
0.78 0.05
etc.

最小工作範例:

\documentclass{article}

\usepackage{datatool}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[x={(210:3cm)},y={(330:3cm)},z={(90:3cm)}]
\draw (0,0,0)--(1,0,0);
\draw (0,0,0)--(0,1,0);
\draw (0,0,0)--(0,0,3);
\DTLsetseparator{ }
\DTLloaddb[noheader=false]{coordinates}{./xz.dat}
\DTLforeach*{coordinates}{\x=x,\z=z}{\filldraw (\x,0,\z) circle (0.02);}
\end{tikzpicture}

\end{document}

結果是:

在此輸入影像描述

是否可以使用平滑的線而不是這些醜陋的壓扁點來連接連續的座標?

我知道如何使用“繪圖檔案”來做到這一點,但這僅適用於二維繪圖。

答案1

即使檔案只有二維座標,也有多種方法可以以 3D 形式繪製檔案。一種方法是新增帶有 的列y=0。當然,您不必手動執行此操作。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{data2.dat}
x z
0.5 0
0.54 0.01
0.58 0.01
0.62 0.02
0.66 0.03
0.7 0.03
0.74 0.04
0.78 0.05
\end{filecontents*}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.16}
\pgfplotstableset{
    create on use/new y/.style={
        create col/expr={0}}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=-0.2,ymax=1]
\addplot3[no marks,smooth] table[x=x,y=new y,z=z] {data2.dat};
\end{axis}
\end{tikzpicture}
\end{document}

在此輸入影像描述

當然,你也可以在您正在使用的框架中新增平滑的繪圖。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{xz.dat}
x z
0.5 0
0.54 0.01
0.58 0.01
0.62 0.02
0.66 0.03
0.7 0.03
0.74 0.04
0.78 0.05
\end{filecontents*}
\usepackage{datatool}
\newcounter{mycoord}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[x={(-25.98mm,-15mm)},y={(25.98mm,-15mm)},z={(0,30mm)}]
\draw (0,0,0)--(1,0,0);
\draw (0,0,0)--(0,1,0);
\draw (0,0,0)--(0,0,3);
\DTLsetseparator{ }
\DTLloaddb[noheader=false]{coordinates}{./xz.dat}
\DTLforeach*{coordinates}{\x=x,\z=z}{\stepcounter{mycoord}
\filldraw (\x,0,\z) coordinate(X-\themycoord) circle (0.02);}
\draw plot[smooth,samples at={1,...,\themycoord},variable=\x] (X-\x);
\end{tikzpicture}

\end{document}

使用你的資料檔案(並擺脫子彈)產量

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{filecontents}
\usepackage{datatool}
\newcounter{mycoord}
\usepackage{tikz}
%\usetikzlibrary{decorations.markings}
\begin{document}

\begin{tikzpicture}[x={(-25.98mm,-15mm)},y={(25.98mm,-15mm)},z={(0,30mm)}]
\draw (0,0,0)--(1,0,0);
\draw (0,0,0)--(0,1,0);
\draw (0,0,0)--(0,0,3);
\DTLsetseparator{ }
\DTLloaddb[noheader=false]{coordinates}{./xz.dat}
\DTLforeach*{coordinates}{\x=x,\z=z}{\stepcounter{mycoord}
\path (\x,0,\z) coordinate(X-\themycoord);}
\draw plot[smooth,tension=0.5,samples at={1,...,\themycoord},variable=\x] (X-\x);
\end{tikzpicture}

\end{document}

在此輸入影像描述

微小的波動來自數據,而不是平滑。

相關內容