2016-02-24 518 views
1

我想要得到如下所示的2D和3D圖。
給出了曲線的方程。
我們如何在Python中做到這一點?
我知道可能有重複,但在發佈時 我無法罰款任何有用的職位。Python:如何圍繞z軸旋轉表面並製作3d圖?

我最初的嘗試是這樣的:

# Imports 
import numpy as np 
import matplotlib.pyplot as plt 


# to plot the surface rho = b*cosh(z/b) with rho^2 = r^2 + b^2 
z = np.arange(-3, 3, 0.01) 
rho = np.cosh(z) # take constant b = 1 

plt.plot(rho,z) 
plt.show() 

一些相關的鏈接如下:
Rotate around z-axis only in plotly

三維積應該是這樣的:
enter image description here

+0

是不是旋轉只是矩陣乘法? – Fabricator

回答

1

好了,所以我認爲你確實需要圍繞軸旋轉2d曲線來創建曲面。我來自CAD背景,所以我就是這樣解釋的。 我不是數學中最偉大的人,所以原諒任何笨重的術語。不幸的是,你必須做數學的其餘部分來獲得網格的所有點。

赫雷什您的代碼:

#import for 3d 
from mpl_toolkits.mplot3d import Axes3D 

import numpy as np 
import matplotlib.pyplot as plt 

變化人氣指數到linspace其捕獲端點否則人氣指數將缺少3.0在陣列的末尾:

z = np.linspace(-3, 3, 600) 
rho = np.cosh(z) # take constant b = 1 

因爲RHO是你在半徑我們需要每個z高度來計算該半徑周圍的x,y點。而在此之前,我們在對半徑什麼位置獲得X,Y座標弄清楚:

#steps around circle from 0 to 2*pi(360degrees) 
#reshape at the end is to be able to use np.dot properly 
revolve_steps = np.linspace(0, np.pi*2, 600).reshape(1,600) 

讓周圍一圈點的觸發方式是:
X = R * COS(THETA )
Y = R * SIN(THETA)

你r爲你的RHO和θ是revolve_steps

使用np.dot做矩陣乘法你得到一個二維數組又回到了X的行和y將對應於z的

theta = revolve_steps 
#convert rho to a column vector 
rho_column = rho.reshape(600,1) 
x = rho_column.dot(np.cos(theta)) 
y = rho_column.dot(np.sin(theta)) 
# expand z into a 2d array that matches dimensions of x and y arrays.. 
# i used np.meshgrid 
zs, rs = np.meshgrid(z, rho) 

#plotting 
fig, ax = plt.subplots(subplot_kw=dict(projection='3d')) 
fig.tight_layout(pad = 0.0) 
#transpose zs or you get a helix not a revolve. 
# you could add rstride = int or cstride = int kwargs to control the mesh density 
ax.plot_surface(x, y, zs.T, color = 'white', shade = False) 
#view orientation 
ax.elev = 30 #30 degrees for a typical isometric view 
ax.azim = 30 
#turn off the axes to closely mimic picture in original question 
ax.set_axis_off() 
plt.show() 

#ps 600x600x600 pts takes a bit of time to render 

我不知道,如果它被固定在matplotlib的最新版本,但與設置3D繪圖的長寬比:

ax.set_aspect('equal') 

沒有工作非常出色。你可以找到解決方案this stack overflow question

+0

對不起,你可以請編輯你的原始問題,並說明你想要的情節完全一樣的圖片。唯一可見的表面線。否則,我覺得我完全回答了這個問題。關於具體的三維視圖,有人說方位和旋轉kwargs的情節我相信。許多方法可以關閉其中一個我認爲是plt.axes_off = True或關閉的其中一個軸。我有很多時間來解決問題的其餘部分。 – Pythonvtr250