通过Sympy中的参数图绘制具有相等轴的完美球体

问题描述 投票:0回答:1

我想知道如何在Sympy中使用plot3d_parametric_surface绘制理想的球体。完美是指相等的轴。此功能的结果为椭圆形!

from sympy import *
from sympy.plotting.plot import plot3d_parametric_surface
from sympy.abc import theta , phi
plot3d_parametric_surface(sin(phi)*cos(theta) , sin(phi)*sin(theta), cos(phi), (phi,0,pi),(theta,0,2*pi))

enter image description here

我试图在matplotlib (equal unit length): with 'equal' aspect ratio z-axis is not equal to x- and y-中实现答案,但没有成功。

python matplotlib sympy aspect-ratio
1个回答
0
投票

主要问题之一是matplotlib中的ax.set_aspect('equal')not implemented。一旦调用set_aspect('equal'),当前版本的matplotlib(3.1)就会引发显式错误。较旧的版本给出了非常错误的预测,尽管通常用户并不知道该错误。关于在3D中设置相等的宽高比的其他StackOverflow答案只是将xyz限制设置为相等,这可以使您更接近所需的结果,但又不是完全相等的投影。

一种解决方法是手动设置图形轴,直到球体具有所需的宽高比。这很麻烦,因为图形尺寸包括轴标签和填充的空间。

例如,将figsize设置为6, 5.6似乎很好

from sympy import sin, cos, pi
from sympy.plotting.plot import plot3d_parametric_surface
from sympy.abc import theta, phi
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = 6, 5.6
plot3d_parametric_surface(sin(phi) * cos(theta), sin(phi) * sin(theta), cos(phi),
                          (phi, 0, pi), (theta, 0, 2 * pi))

resulting plot

仅通过对角线插入一个不可见的框(以防您未绘制球体的情况:

from sympy import sin, cos, pi
from sympy.plotting.plot import plot3d_parametric_surface, plot3d_parametric_line
from sympy.abc import theta, phi, t
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = 6, 5.6

p1 = plot3d_parametric_surface(sin(phi) * cos(theta), sin(phi) * sin(theta), cos(phi),
                               (phi, 0, pi), (theta, 0, 2 * pi), show=False)
p2 = plot3d_parametric_line(t, t, t, (t, -1, 1), line_color='none', show=False)
p1.append(p2[0])

#p1.backend(p1).ax[0].set_aspect('equal')  # this raises a NotImplementedError
p1.show()
© www.soinside.com 2019 - 2024. All rights reserved.