有沒有辦法用 pgfplots 繪製 Voronoi 圖?

有沒有辦法用 pgfplots 繪製 Voronoi 圖?

我想用 pgfplots 繪製一個 voronoi 圖,但遺憾的是我無法得到它應有的樣子。我使用 Matlab 命令來取得 x 和 y 座標並將它們儲存為 dat 檔案。在該檔案中,我將 x 座標儲存在第一列中,將 y 座標儲存在第二列中。

Matlab 輸出:我希望如何擁有它

pgfplots 輸出:快照現在的樣子

有誰知道它如何運作?

程式碼的最小範例:

\documentclass{standalone}

\usepackage{amsmath,amsfonts,amssymb} 
\usepackage{mathtools} 
\usepackage[ngerman]{babel} 
\usepackage[utf8]{inputenc} 
\usepackage{graphicx} 
\usepackage[svgnames]{xcolor} 
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usepackage{tikz} 
\usetikzlibrary{positioning,fit,calc,shapes,arrows,external,3d,patterns,spy}

\usepackage{filecontents}

\begin{document}

\begin{filecontents*}{data.dat}
  -7.9265104e+00   3.6503989e-01
   4.3133971e-01   9.4062409e-01
  -7.9265104e+00   3.6503989e-01
   6.3976919e-02   5.4947497e-01
   6.3976919e-02   5.4947497e-01
   3.0293735e-01   6.5037024e-01
   3.6748619e-01   9.5786833e-02
   6.3976919e-02   5.4947497e-01
   3.0293735e-01   6.5037024e-01
   4.0645496e-01   8.2958752e-01
   4.9066479e-01   4.4410036e-01
   3.0293735e-01   6.5037024e-01
   3.6748619e-01   9.5786833e-02
   4.9244296e-01   2.3488102e-01
   4.9244296e-01   2.3488102e-01
   4.9066479e-01   4.4410036e-01
   4.9066479e-01   4.4410036e-01
   7.3082954e-01   5.1825327e-01
   1.8829507e+00   1.7053562e-01
   4.9244296e-01   2.3488102e-01
   4.3133971e-01   9.4062409e-01
   8.7230061e-01   2.0371867e+00
   4.0645496e-01   8.2958752e-01
   4.3133971e-01   9.4062409e-01
   6.6251849e-01   6.4842135e-01
   4.0645496e-01   8.2958752e-01
   6.9696115e-01   6.8938388e-01
   8.7230061e-01   2.0371867e+00
   7.3082954e-01   5.1825327e-01
   6.6251849e-01   6.4842135e-01
   1.8829507e+00   1.7053562e-01
   7.3082954e-01   5.1825327e-01
   6.6251849e-01   6.4842135e-01
   6.9696115e-01   6.8938388e-01
  -7.9265104e+00   3.6503989e-01
  -1.7371176e+01   8.6111447e-02
   3.6748619e-01   9.5786833e-02
  -6.0998602e-02  -1.3709259e+00
   1.8829507e+00   1.7053562e-01
   4.4291165e+00  -2.9045575e-01
   8.7230061e-01   2.0371867e+00
   1.8287612e+00   4.5227847e+00
   6.9696115e-01   6.8938388e-01
   1.9370861e+00   1.3452721e+00
\end{filecontents*}

\begin{tikzpicture}
\begin{axis}[x=3cm, y=3cm, z=0cm, view={0}{90}]
\addplot3 [color=blue, samples y=0] file {voronoi.dat}; 
\end{axis}
\end{tikzpicture}

\end{document}

感謝您的幫忙!

答案1

您需要在 Voronoi 圖的各個線段之間添加一條空線,以便線段不相連。

假設您有以下檔案“points.dat”,其中包含要為其繪製 Voronoi 圖的點的座標:

1   4
4   1
2   6
3   5
4   3
6   2

然後你可以使用 Matlab 或 Octave 找到 Voronoi 圖,如下所示:

points = dlmread('points.dat');
[vx, vy] = voronoi(points(:,1), points(:,2));

要將其寫入可以使用 PGFPlots 繪製的文件,您可以使用

fid = fopen('voronoi.dat', 'w');
fprintf(fid, '%f %f\n%f %f\n\n', [vx(:), vy(:)]');
fclose(fid);

在 PGFPlots 中,您可以像這樣建立繪圖:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis equal image
]
\addplot [only marks, red] table {points.dat};
\addplot [no markers, update limits=false] table {voronoi.dat};
\end{axis}
\end{tikzpicture}
\end{document}

相關內容