Python:如何围绕z轴旋转曲面并绘制3d图? [英] Python: How to revolve a surface around z axis and make a 3d plot?

查看:1005
本文介绍了Python:如何围绕z轴旋转曲面并绘制3d图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得2d和3d图,如下所示.
给出了曲线方程.
如何在python中这样做?
我知道可能有重复,但在发布时 我无法罚款任何有用的帖子.

I want to get 2d and 3d plots as shown below.
The equation of the curve is given.
How can we do so in python?
I know there may be duplicates but at the time of posting I could not fine any useful posts.

我最初的尝试是这样的:

My initial attempt is like this:

# 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()

一些相关链接如下:
仅在绘图中围绕z轴旋转

Some related links are following:
Rotate around z-axis only in plotly

3d图应如下所示:

The 3d-plot should look like this:

推荐答案

好,所以我认为您真的是在要求绕轴旋转2d曲线以创建曲面.我来自CAD背景,所以这就是我的解释方式. 而且我不是最擅长数学的人,因此请原谅任何笨拙的术语.不幸的是,您必须完成其余的数学运算才能获得网格的所有点.

Ok so I think you are really asking to revolve a 2d curve around an axis to create a surface. I come from a CAD background so that is how i explain things. and I am not the greatest at math so forgive any clunky terminology. Unfortunately you have to do the rest of the math to get all the points for the mesh.

在这里输入您的代码:

#import for 3d
from mpl_toolkits.mplot3d import Axes3D

import numpy as np
import matplotlib.pyplot as plt

将arange更改为linspace会捕获端点,否则arange会在数组末尾缺少3.0:

change arange to linspace which captures the endpoint otherwise arange will be missing the 3.0 at the end of the array:

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

由于rho是您在每个z高度处的半径,因此我们需要计算该半径周围的x,y点.在此之前,我们必须找出该半径上的哪些位置以获得x,y坐标:

since rho is your radius at every z height we need to calculate x,y points around that radius. and before that we have to figure out at what positions on that radius to get x,y co-ordinates:

#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)

获取绕圆点的Trig方法是:
x = r * cos(theta)
y = r * sin(theta)

the Trig way of getting points around a circle is:
x = r*cos(theta)
y = r*sin(theta)

因为你是你的rho,theta是revolve_steps

for you r is your rho, and theta is revolve_steps

通过使用np.dot进行矩阵乘法,您将获得一个二维数组,其中x和y的行将与z的行相对应

by using np.dot to do matrix multiplication you get a 2d array back where the rows of x's and y's will correspond to the z's

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图的纵横比:

I am not sure if it's been fixed in latest version of matplotlib but the setting the aspect ratio of 3d plots with:

ax.set_aspect('equal')

效果不是很好.您可以在此堆栈溢出问题

has not worked very well. you can find solutions at this stack overflow question

这篇关于Python:如何围绕z轴旋转曲面并绘制3d图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆