How to Plot a 3D Surface from Numerical Data in Mathematica?

15 replies · 3K views · last reply
Telemachus
Messages
820
Reaction score
30
Hi there. I wanted to plot a surface, given implicitly by a function ##f(x,y,z)=c##, with ##c## a constant. My ##f(x,y,z)## is obtained numerically, so I don't know the expression explicitly, I have the values of ##f(x_i,y_j,z_k)## for given integer numbers ##(i,j,k)##, ##i=1,2,3...## same for ##j## and ##k##. And the discrete variables are ##x_i=(i-1)\Delta x## and similarly for ##y_j,z_k##.

So, in principle I can obtain, numerically a set of data for which ##f(x_i,y_j,z_k)=c##, where the ##x_i,y_j,z_k## are stored, and I know c (I just choose the value I want). And now I know that for those values, the magnitude of ##f(x,y,z)=c##, so this defines a 3D surface. Now, how can I plot this? it should be clear at this point that f might not define explicitly a function, it might not be bijective, it is likely to be a closed surface, like a spherical surface in 3D.
 
can you use Matlab?
 
  • Like
Likes   Reactions: Telemachus
Yes, I can. But after thinking of it a bit, I think I should just plot my set of points, right? the obtained ##(x_i,y_j,z_k)## are the ones that defines my surface. Right?
 
Last edited:
Actually, you can do that, but I don't think it's a "professional" way. If you do it, the figure you obtained will be a lot of dots :biggrin:
How about interpolating and approximating that function, then use MATLAB to plot it? :wink:
 
  • Like
Likes   Reactions: Telemachus
Yes, that's a good idea. Is there a way to do it in MATLAB automatically? a way in which it plots the points, and makes the surface smooth by interpolation?
 
As I know, MATLAB can't do it automatically, we must code an interpolation program to interpolate and approximate that function :frown:
 
  • Like
Likes   Reactions: Telemachus
Ok. So here I have a set of data which I want to plot. I have also attached a plot of the data:

Gnuplot 3D scatter plot of purple plus-sign points labelled 'sur1001.dat', spread over a box with axes from 0.25 to 0.75 and vertical values from 0 to 0.25.


So, as there are not many points, I really have to use some interpolation to get a descent picture. However, I don't think that even after the interpolation I will be able to plot it as a surface, I haven't found how to do it, but maybe if I have more points after interpolation, I can make it look more descent.

I was thinking to take pairs of points: (x,y), (x,z), (y,z) and make an interpolation for each pair. After that I can collect the whole bunch of data intro a single file back again, and plot it just like that. The idea is to have interpolations over the three planes.
 
Attachments
  • sur1001.txt
    sur1001.txt
    5.4 KB · Views: 526
  • Gnuplot 3D scatter plot of 'sur1001.dat' showing about 40 purple cross markers scattered in a box with axes running from 0.25 to 0.75 on two horizontal sides and 0 to 0.25 on the vertical axis.
    surface.png
    5.1 KB · Views: 464
  • Gnuplot 3D scatter plot of purple plus-sign points labelled 'sur1001.dat', spread over a box with axes from 0.25 to 0.75 and vertical values from 0 to 0.25.
    ?temp_hash=fe4d2b10be01ef0dbe351705d9a7e895.png
    5.1 KB · Views: 521
Very easy in Mathematica. Here's the code to collect points {x,y,sin(x y)} in range (-2,2) and then I superimpose the actual plot (red) against the interpolation (blue):
Code:
p1 = Plot3D[Sin[x y], {x, -2, 2}, {y, -2, 2}, PlotStyle -> Red];
mySinTable =
  Flatten[Table[{x, y, Sin[x y]}, {x, -2, 2, 0.1}, {y, -1, 1, 0.1}],
   1];
mySinInterpolation = Interpolation[mySinTable];
p2 = Plot3D[mySinInterpolation[x , y], {x, -2, 2}, {y, -2, 2},
   PlotStyle -> Blue];
Show[{p1, p2}]
3D surface plot of sin(xy) over x and y from -2 to 2, with a red exact surface overlaid on a blue interpolated surface that mostly matches it; z-axis ranges from -1 to 1.
 
Attachments
  • 3D surface plot of sin(xy) over x and y from -2 to 2, with a red exact surface overlaid on a blue interpolated surface that mostly matches it; z-axis ranges from -1 to 1.
    interpolation of sin.png
    23.7 KB · Views: 627
  • Like
Likes   Reactions: Telemachus
Can you do that with the data I have provided? I am wondering if the fact that this data isn't a function ##z=f(x,y)## (not bijective) generates any issue in mathematica. If possible the interpolation has to be done in many directions. If you, for example, only interpolate along the fixed z curves, it will look poorly along the z axis, and the same applies for the other planes.

Thanks!
 
Telemachus said:
Can you do that with the data I have provided? I am wondering if the fact that this data isn't a function ##z=f(x,y)## (not bijective) generates any issue in mathematica. If possible the interpolation has to be done in many directions. If you, for example, only interpolate along the fixed z curves, it will look poorly along the z axis, and the same applies for the other planes.

Thanks!

Post the data as a table {{x1,y1,z1},...,{xn,yn,zn}} and I'll try to run it through the code above and see what happens.
 
  • Like
Likes   Reactions: Telemachus
Is it okey like this?

0.44736842105263153 0.73684210526315785 0.28947368421052633
0.47368421052631576 0.73684210526315785 0.28947368421052633
0.50000000000000000 0.73684210526315785 0.28947368421052633
0.52631578947368418 0.73684210526315785 0.28947368421052633
0.55263157894736836 0.73684210526315785 0.28947368421052633
0.47368421052631576 0.76315789473684204 0.28947368421052633
0.50000000000000000 0.76315789473684204 0.28947368421052633
0.52631578947368418 0.76315789473684204 0.28947368421052633
0.44736842105263153 0.68421052631578938 0.31578947368421051
0.55263157894736836 0.68421052631578938 0.31578947368421051
0.39473684210526316 0.78947368421052633 0.31578947368421051
0.60526315789473684 0.78947368421052633 0.31578947368421051
0.44736842105263153 0.65789473684210520 0.34210526315789469
0.55263157894736836 0.65789473684210520 0.34210526315789469
0.34210526315789469 0.71052631578947367 0.34210526315789469
0.65789473684210520 0.71052631578947367 0.34210526315789469
0.34210526315789469 0.78947368421052633 0.34210526315789469
0.65789473684210520 0.78947368421052633 0.34210526315789469
0.39473684210526316 0.81578947368421051 0.34210526315789469
0.42105263157894735 0.81578947368421051 0.34210526315789469
0.44736842105263153 0.81578947368421051 0.34210526315789469
0.55263157894736836 0.81578947368421051 0.34210526315789469
0.57894736842105265 0.81578947368421051 0.34210526315789469
0.60526315789473684 0.81578947368421051 0.34210526315789469
0.47368421052631576 0.63157894736842102 0.36842105263157893
0.50000000000000000 0.63157894736842102 0.36842105263157893
0.52631578947368418 0.63157894736842102 0.36842105263157893
0.34210526315789469 0.68421052631578938 0.36842105263157893
0.65789473684210520 0.68421052631578938 0.36842105263157893
0.31578947368421051 0.71052631578947367 0.36842105263157893
0.68421052631578938 0.71052631578947367 0.36842105263157893
0.31578947368421051 0.78947368421052633 0.36842105263157893
0.68421052631578938 0.78947368421052633 0.36842105263157893
0.42105263157894735 0.63157894736842102 0.39473684210526316
0.57894736842105265 0.63157894736842102 0.39473684210526316
0.34210526315789469 0.81578947368421051 0.39473684210526316
0.65789473684210520 0.81578947368421051 0.39473684210526316
0.34210526315789469 0.65789473684210520 0.42105263157894735
0.65789473684210520 0.65789473684210520 0.42105263157894735
0.31578947368421051 0.68421052631578938 0.42105263157894735
0.68421052631578938 0.68421052631578938 0.42105263157894735
0.44736842105263153 0.84210526315789469 0.42105263157894735
0.47368421052631576 0.84210526315789469 0.42105263157894735
0.50000000000000000 0.84210526315789469 0.42105263157894735
0.52631578947368418 0.84210526315789469 0.42105263157894735
0.55263157894736836 0.84210526315789469 0.42105263157894735
0.36842105263157893 0.63157894736842102 0.44736842105263153
0.63157894736842102 0.63157894736842102 0.44736842105263153
0.28947368421052633 0.71052631578947367 0.44736842105263153
0.71052631578947367 0.71052631578947367 0.44736842105263153
0.42105263157894735 0.84210526315789469 0.44736842105263153
0.44736842105263153 0.84210526315789469 0.44736842105263153
0.47368421052631576 0.84210526315789469 0.44736842105263153
0.50000000000000000 0.84210526315789469 0.44736842105263153
0.52631578947368418 0.84210526315789469 0.44736842105263153
0.55263157894736836 0.84210526315789469 0.44736842105263153
0.57894736842105265 0.84210526315789469 0.44736842105263153
0.47368421052631576 0.60526315789473684 0.47368421052631576
0.50000000000000000 0.60526315789473684 0.47368421052631576
0.52631578947368418 0.60526315789473684 0.47368421052631576
0.36842105263157893 0.63157894736842102 0.47368421052631576
0.63157894736842102 0.63157894736842102 0.47368421052631576
0.28947368421052633 0.71052631578947367 0.47368421052631576
0.71052631578947367 0.71052631578947367 0.47368421052631576
0.42105263157894735 0.84210526315789469 0.47368421052631576
0.44736842105263153 0.84210526315789469 0.47368421052631576
0.55263157894736836 0.84210526315789469 0.47368421052631576
0.57894736842105265 0.84210526315789469 0.47368421052631576
0.47368421052631576 0.60526315789473684 0.50000000000000000
0.50000000000000000 0.60526315789473684 0.50000000000000000
0.52631578947368418 0.60526315789473684 0.50000000000000000
0.39473684210526316 0.84210526315789469 0.50000000000000000
0.42105263157894735 0.84210526315789469 0.50000000000000000
0.57894736842105265 0.84210526315789469 0.50000000000000000
0.60526315789473684 0.84210526315789469 0.50000000000000000
0.47368421052631576 0.60526315789473684 0.52631578947368418
0.50000000000000000 0.60526315789473684 0.52631578947368418
0.52631578947368418 0.60526315789473684 0.52631578947368418
0.36842105263157893 0.63157894736842102 0.52631578947368418
0.63157894736842102 0.63157894736842102 0.52631578947368418
0.28947368421052633 0.71052631578947367 0.52631578947368418
0.71052631578947367 0.71052631578947367 0.52631578947368418
0.42105263157894735 0.84210526315789469 0.52631578947368418
0.44736842105263153 0.84210526315789469 0.52631578947368418
0.55263157894736836 0.84210526315789469 0.52631578947368418
0.57894736842105265 0.84210526315789469 0.52631578947368418
0.36842105263157893 0.63157894736842102 0.55263157894736836
0.63157894736842102 0.63157894736842102 0.55263157894736836
0.28947368421052633 0.71052631578947367 0.55263157894736836
0.71052631578947367 0.71052631578947367 0.55263157894736836
0.42105263157894735 0.84210526315789469 0.55263157894736836
0.44736842105263153 0.84210526315789469 0.55263157894736836
0.47368421052631576 0.84210526315789469 0.55263157894736836
0.50000000000000000 0.84210526315789469 0.55263157894736836
0.52631578947368418 0.84210526315789469 0.55263157894736836
0.55263157894736836 0.84210526315789469 0.55263157894736836
0.57894736842105265 0.84210526315789469 0.55263157894736836
0.34210526315789469 0.65789473684210520 0.57894736842105265
0.65789473684210520 0.65789473684210520 0.57894736842105265
0.31578947368421051 0.68421052631578938 0.57894736842105265
0.68421052631578938 0.68421052631578938 0.57894736842105265
0.44736842105263153 0.84210526315789469 0.57894736842105265
0.47368421052631576 0.84210526315789469 0.57894736842105265
0.50000000000000000 0.84210526315789469 0.57894736842105265
0.52631578947368418 0.84210526315789469 0.57894736842105265
0.55263157894736836 0.84210526315789469 0.57894736842105265
0.42105263157894735 0.63157894736842102 0.60526315789473684
0.57894736842105265 0.63157894736842102 0.60526315789473684
0.34210526315789469 0.81578947368421051 0.60526315789473684
0.65789473684210520 0.81578947368421051 0.60526315789473684
0.47368421052631576 0.63157894736842102 0.63157894736842102
0.50000000000000000 0.63157894736842102 0.63157894736842102
0.52631578947368418 0.63157894736842102 0.63157894736842102
0.34210526315789469 0.68421052631578938 0.63157894736842102
0.65789473684210520 0.68421052631578938 0.63157894736842102
0.31578947368421051 0.71052631578947367 0.63157894736842102
0.68421052631578938 0.71052631578947367 0.63157894736842102
0.31578947368421051 0.78947368421052633 0.63157894736842102
0.68421052631578938 0.78947368421052633 0.63157894736842102
0.44736842105263153 0.65789473684210520 0.65789473684210520
0.55263157894736836 0.65789473684210520 0.65789473684210520
0.34210526315789469 0.71052631578947367 0.65789473684210520
0.65789473684210520 0.71052631578947367 0.65789473684210520
0.34210526315789469 0.78947368421052633 0.65789473684210520
0.65789473684210520 0.78947368421052633 0.65789473684210520
0.39473684210526316 0.81578947368421051 0.65789473684210520
0.42105263157894735 0.81578947368421051 0.65789473684210520
0.44736842105263153 0.81578947368421051 0.65789473684210520
0.55263157894736836 0.81578947368421051 0.65789473684210520
0.57894736842105265 0.81578947368421051 0.65789473684210520
0.60526315789473684 0.81578947368421051 0.65789473684210520
0.44736842105263153 0.68421052631578938 0.68421052631578938
0.55263157894736836 0.68421052631578938 0.68421052631578938
0.39473684210526316 0.78947368421052633 0.68421052631578938
0.60526315789473684 0.78947368421052633 0.68421052631578938
0.44736842105263153 0.73684210526315785 0.71052631578947367
0.47368421052631576 0.73684210526315785 0.71052631578947367
0.50000000000000000 0.73684210526315785 0.71052631578947367
0.52631578947368418 0.73684210526315785 0.71052631578947367
0.55263157894736836 0.73684210526315785 0.71052631578947367
0.47368421052631576 0.76315789473684204 0.71052631578947367
0.50000000000000000 0.76315789473684204 0.71052631578947367
0.52631578947368418 0.76315789473684204 0.71052631578947367
 
That will do. Just paste into myNumList as a string and (basically spherical):

Code:
myList = ReadList[StringToStream[myNumList], Record];
myDataPoints =
  ReadList[StringToStream[#], Record, RecordSeparators -> " "] & /@
   myList;
data2 = ToExpression[#] & /@ myDataPoints;
ListPointPlot3D[data2, BoxRatios -> {1, 1, 1}]
ListSurfacePlot3D[data2, BoxRatios -> {1, 1, 1}]

gives:
Left: scatter plot of blue points in a 3D box, axes from 0.3 to 0.7 and 0.8, forming a hollow spherical shell. Right: orange shaded wireframe surface reconstructed from the same data, shown as a rounded blob with a protrusion.
 
Attachments
  • Left: scatter plot of blue points in a 3D box, axes from 0.3 to 0.7 and 0.8, forming a hollow spherical shell. Right: orange shaded wireframe surface reconstructed from the same data, shown as a rounded blob with a protrusion.
    plotforpf.png
    40.2 KB · Views: 978
  • Like
Likes   Reactions: Telemachus
Thanks a lot! I think I should make some interpolation anyway to get it nicer, or maybe run it with a finer grid size (which is certainly too computationally expensive). But that's exactly what I wanted. Thank you!
 
Is that really a 3D plot?
I mean you give the x,y,z values but at each point you have a value for f... That's a 4D plot (that can be visualized with a gradient colored 3D plot)
 
  • Like
Likes   Reactions: Telemachus
Yes. The plot of f(x,y,z) is four dimensional, because if you want to plot it, you would have to plot f(x,y,z) for each value of (x,y,z) But now I am fixing the value of f(x,y,z), f(x,y,z)=constant. So I have one dimension less, and what I am plotting is the (x,y,z) for which f(x,y,z)=some constant.
 
aheight said:
That will do. Just paste into myNumList as a string and (basically spherical):

Code:
myList = ReadList[StringToStream[myNumList], Record];
myDataPoints =
  ReadList[StringToStream[#], Record, RecordSeparators -> " "] & /@
   myList;
data2 = ToExpression[#] & /@ myDataPoints;
ListPointPlot3D[data2, BoxRatios -> {1, 1, 1}]
ListSurfacePlot3D[data2, BoxRatios -> {1, 1, 1}]

gives:
View attachment 224872

Hi. I was finally trying to use this. However, I did not know how to introduce the list of numbers.

Could you exemplify with a list of a few numbers?

I get an error when I try to load the number list. Does mathematica has an issue with spaces between numbers? and is there a way to make mathematica interpret, for example, somethingE-002 as something##\times 10^{-2}##? I think I have an issue with that too.

My list of numbers looks like this now:

" 0.47368421052631576 , 0.10526315789473684 , 7.8947368421052627E-002 "
" 0.50000000000000000 , 0.10526315789473684 , 7.8947368421052627E-002 "
" 0.52631578947368418 , 0.10526315789473684 , 7.894736842052627E-002 "

And so on. There are blank spaces between the commas, but the forum ignores them.

I've defined at the beginning: myNumList= (And the list of numbers here).Thanks!