Home > Back-end >  generate a 3d plot from data contained in a three columns file
generate a 3d plot from data contained in a three columns file

Time:12-24

I have a three columns data file structured in this way (example) :

X Y Z

0 0 0.2 
0 1 0.3
0 2 0.1
1 0 0.2
1 1 0.3
1 2 0.9
2 0 0.6
2 1 0.8
2 2 0.99

I don't know how this kind of file is called ... but I did not find any example to plot this using 3d wireframe or 3d surface plot ...

EDIT But there is a way to produce a wireframe or surface plot with data structured in this way?

CodePudding user response:

In order to create a surface plot, you first need to transform your x, y and z data into 2d arrays. Then you can plot it easily:

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

# read out data from txt file
data = np.genfromtxt("data.txt")[1:]
x_data, y_data, z_data = data[:, 0], data[:, 1], data[:, 2]

# initialize a figure for the 3d plot
fig = plt.figure()
ax = Axes3D(fig)

# create matrix for z values
dim = int(np.sqrt(len(x_data)))
z = z_data.reshape((dim, dim))

# create matrix for the x and y points
x, y = np.arange(0, dim, 1), np.arange(0, dim, 1)
x, y = np.meshgrid(x, y)

# plot
ax.plot_surface(x, y, z, alpha=0.75)
plt.show()
  • Related