从外部坐标文件绘制三维平滑曲线

从外部坐标文件绘制三维平滑曲线

我想根据保存在文件中的先前计算的坐标(例如“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

即使文件只有二维坐标,也有很多方法可以将其绘制成三维图像。一种方法是添加一个带有 的列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}

在此处输入图片描述

这些小波动是由数据引起的,而不是由平滑引起的。

相关内容