我有一個以這種方式構建的三列資料檔案(示例):
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
我不知道如何呼叫這種檔案......但我沒有找到任何使用 3d 線框或 3d 曲面圖繪制它的示例......
編輯但是有一種方法可以使用以這種方式構建的資料生成線框或曲面圖嗎?
uj5u.com熱心網友回復:
為了創建曲面圖,您首先需要將 x、y 和 z 資料轉換為二維陣列。然后你可以輕松地繪制它:
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/394392.html
標籤:Python matplotlib 阴谋
上一篇:將共享軸標簽放在上圖
