Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot surface without using meshgrid

How can I plot surface of data that I have in the form below.

Column 1 - X; Colum 2 - Y; Column 3 - Z;

In the example below there are 4 distinctive points for X and 3 for Y, however I cannot predict that, and I would have to analyze data each time to determine how to reshape columns into grid. Can I plot points just as they are? (A list of coordinates.)

[[0.         0.         0.        ]
 [0.         0.5        0.6989218 ]
 [0.         1.         0.87790919]
 [0.25       0.         0.0505097 ]
 [0.25       0.5        0.7494315 ]
 [0.25       1.         0.92841889]
 [0.5        0.         0.09192357]
 [0.5        0.5        0.79084537]
 [0.5        1.         0.96983276]
 [0.75       0.         0.10310818]
 [0.75       0.5        0.80202997]
 [0.75       1.         0.98101736]
 [1.         0.         0.12209081]
 [1.         0.5        0.82101261]
 [1.         1.         1.        ]]
like image 354
user7810882 Avatar asked Jan 20 '26 10:01

user7810882


2 Answers

If I understood your comments correctly, you are basically looking for plot_trisurf. Here data is your data matrix where I am taking first, second and third columns as x, y, z data respectively.

You don't need any reshaping for this. The input for plot_trisurf are 1-d arrays.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# data = # your matrix here

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection="3d")

x = data[:, 0]
y = data[:, 1]
z = data[:, 2]

ax.plot_trisurf(x,y,z)

enter image description here

like image 138
Sheldore Avatar answered Jan 23 '26 00:01

Sheldore


In matplotlib.pyplot there is an option for scatter for 3 dims. Something like:

import matplotlib.pyplot as plt
plt.figure()
plt.scatter(x, y, z)
plt.show()

Here is documentation.

edit: For 3d plotting you can try the mpl_toolkits.mplot3d library as follows:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
Axes3D.plot_surface(X, Y, Z, *args, **kwargs)
like image 33
slayer Avatar answered Jan 23 '26 00:01

slayer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!