       Re: Plotting 3D Surface Plots using data from an external file

• To: MathGroup at yoda.physics.unc.edu
• Subject: Re: Plotting 3D Surface Plots using data from an external file
• From: physjfh at phys.canterbury.ac.nz (Jason Harris)
• Date: Tue, 29 Mar 1994 23:17:24 +1200

```Dear Mathgroupers,

In a previous post John <jlane at what.sps.mot.com> asks

>I am trying to plot a 3D surface graph using data in a file of the form:

>                  x1  y1  z1
>                  x2  y2  z2
>                      .
>                      .
>                  xN  yN  zN

>This is the way that a plotting program called GENPLOT likes it.

>I have tried the following which, however, does not work:

>         <<Graphics'Graphics3D'
>         ListSurfacePlot3D[data]

After some communication with John I came up with a small package that
will solve the above problem. Basically the problem is that the data is
not in the form of a structured array.

The package List To Array will take a list of 3D points and turn them
into a sorted structured array. It will take the points in any order.

Some Mathematica graphing functions use arrays so this is a general
program for interfacing the output from sources other than Mathematica
files. Some common functions that use arrays are

: ListSurfacePlot3D
: ListPlot3D
: BarChart3D
: ListContourPlot
: ListDensityPlot

The text of the the package is included at the end of this post.

An example use might be

-------------

<<Graphics`Graphics3D`

<<ListToArray.ma

arrayData=ListToArray[data]

ListSurfacePlot3D[data]

-------------
just as a note:

If the points do not lie on a regular rectangular grid one may use the
function TriangularSurfacePlot in the package
DiscreteMath`ComputationalGeometry` but this is MUCH MUCH slower.

text of ListToArray follows

j.harris at phys.canterbury.ac.nz
----------------------------------------

(*:Version: Mathematica 2.0 *)

(*:Name: ListToArray` *)

(*:Title: ListToArray *)

(*:Author: Jason F. Harris *)

(*:Keywords:
Array, 3D graphics, ListSurfacePlot3D, ListPlot3D,
ListDensityPlot, Interfaciing, GENPLOT *)

(*:Requirements: *)

(*:Warnings:
For further use of the output of ListToArray the input
points must lie on a regular rectangular grid in the x-y
plane, otherwise the Mathematica functions requiring Arrays
will not work with this output. *)

(*:Source:
motivated by questions from
John Lane, Motorola DSP Division
PCMedia Operation
jlane at what.sps.mot.com
*)

(*:Limitations: none. *)

(*:Discussion:

This function is useful for 3D graphics interfacing.

Certain Mathematica graphing functions require their data
to be in the form of a array of 3D points. ListToArray
will transform a list of 3D points in any order into a
sorted structured array of 3D points valid for
BarChart3D, ListContourPlot, ListDensityPlot and other
Mathematica Graphing functions.

If the points do not lie on a regular rectangular grid one
may use the function TriangularSurfacePlot in the package
DiscreteMath`ComputationalGeometry` but this is MUCH MUCH
slower.

*)

(* :Implementation:
ListToArray first sorts the points into the right order.
It then reforms the List of points into the List of Lists
of points that is the array.
*)

(* :Examples:

This example assumes that the datafile contains data
in the form x1 y1 z1 x2 y2 z2 x3 y3 z3 etc ...
with no particular ordering on the xi's or yi's and
returns anywhere.

arrayData = ListToArray[data];

ListSurfacePlot3D[arrayData];

*)

BeginPackage["ListToArray`"];

ListToArray::usage = "ListToArray[{{x1,y1,z1},{x2,y2,z2},...}]
will transform a list of 3D points into a sorted structured array
needed for certain 3D graphing functions in Mathematica.";

Begin["`Private`"];

ListToArray[a_List]:=
reform[gridSort[a]]

(*-------------------------------*)

(* These two functions will sort the list into the order of
an array *)

myPredicate[{x1_,y1_,z1_},{x2_,y2_,z2_}]:=
(x1<x2) || (x1==x2 && (y1<=y2))

gridSort[a_List]:=Sort[a,myPredicate[#1,#2]&]

(*-------------------------------*)

(* This will transform the list of points to the
appropriate list of lists of points. *)

reform[a_List]:=Module[{xValues},
xValues=Union[Flatten[Cases[a,{x_,y_,z_}->x]]];
Print[xValues];
Map[(Cases[a,{#,y_,z_}])&,xValues]]

(*-------------------------------*)

End[];   (* ListToArray`Private` *)

Protect[ListToArray];

EndPackage[];   (* ListToArray` *)

```

• Prev by Date: ISSAC'94 ISSAC'94 St Catherine's College, Oxford, UK 20-22 July 1994 Preliminary Conference Registration Notes
• Next by Date: Re: Using substitution rules to define a function
• Previous by thread: Plotting 3D Surface Plots using data from an external file
• Next by thread: Thank Yous