簡體   English   中英

將圖轉換為表面圖,matplotlib?

[英]Convert plot to a surface plot, matplotlib?

贏得10 x64 Anaconda Python 2.7

我使用以下代碼在高斯曲面上繪制漸開線螺旋。

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Spiral parameters
samNum = 1000
spConst = 10.0
t = np.linspace(0, 6*np.pi, samNum)

# Coordinates of involute spiral on xy-plane
coords = np.zeros([samNum, 3])
coords[:,0] = spConst * (np.cos(t) + t * np.sin(t)) # x coord
coords[:,1] = spConst * (np.sin(t) - t * np.cos(t)) # y coord

# Paramters for 2D Gaussian surface 
amp = 200
sigma_x = 75.0
sigma_y = 75.0
theta = np.pi
a = np.cos(theta)**2 / (2 * sigma_x**2) + np.sin(theta)**2 / (2 * sigma_y**2)
b = -np.sin(2 * theta) / (4 * sigma_x**2) + np.sin(2 * theta) / (4 * sigma_y**2)
c = np.sin(theta)**2 / (2 * sigma_x**2) + np.cos(theta)**2 / (2 * sigma_y**2)

# z coords of spiral projected onto Gaussian surface
coords[:,2] = amp * np.exp(-(a * coords[:,0]**2 - 2 * b * coords[:,0]*coords[:,1] + c * coords[:,1]**2)) # z coord

# plot 3D spiral
ax.scatter(coords[:,0], coords[:,1], coords[:,2],  s=1, c='k')

# plot lines projecting 3D spiral on to the xy-plane
for p in range(samNum):  
    ax.plot([coords[p,0], coords[p,0]], [coords[p,1], coords[p,1]], [0, coords[p,2]], color='g', linewidth=0.1)

ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

這給出了以下輸出...

在此處輸入圖片說明

我想將綠絲帶轉換為連續的表面。 我看過matplotlib中的參數化曲面,但無法理解如何將其隱藏到曲面中。

那有可能嗎? 任何指針表示贊賞。

原則上,您已經擁有了所需的一切,

t = np.linspace(0, 6*np.pi, samNum)

T, Z = np.meshgrid(t, [0,1])
X = spConst * (np.cos(T) + T* np.sin(T))
Y = spConst * (np.sin(T) - T * np.cos(T))

給出XY坐標,通過Z[1,:] = coords[:,2]獲得上Z坐標。

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Spiral parameters
samNum = 1000
spConst = 10.0
t = np.linspace(0, 6*np.pi, samNum)

T, Z = np.meshgrid(t, [0,1])
X = spConst * (np.cos(T) + T* np.sin(T))
Y = spConst * (np.sin(T) - T * np.cos(T))

# Coordinates of involute spiral on xy-plane
coords = np.zeros([samNum, 3])
coords[:,0] = spConst * (np.cos(t) + t * np.sin(t)) # x coord
coords[:,1] = spConst * (np.sin(t) - t * np.cos(t)) # y coord

# Paramters for 2D Gaussian surface 
amp = 200
sigma_x = 75.0
sigma_y = 75.0
theta = np.pi
a = np.cos(theta)**2 / (2 * sigma_x**2) + np.sin(theta)**2 / (2 * sigma_y**2)
b = -np.sin(2 * theta) / (4 * sigma_x**2) + np.sin(2 * theta) / (4 * sigma_y**2)
c = np.sin(theta)**2 / (2 * sigma_x**2) + np.cos(theta)**2 / (2 * sigma_y**2)

# z coords of spiral projected onto Gaussian surface
coords[:,2] = amp * np.exp(-(a * coords[:,0]**2 - 2 * b * coords[:,0]*coords[:,1] + c * coords[:,1]**2)) # z coord

Z[1,:] = coords[:,2] 
ax.plot_surface(X,Y,Z)

# plot 3D spiral
ax.scatter(coords[:,0], coords[:,1], coords[:,2],  s=1, c='k')


ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.show()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM