在Python的matplotlib中绘制球面上的点

时间:2021-08-16 06:01:05

I'm trying to generate a plot of a sphere, with some points plotted on the surface of the sphere. (Specifically the points are the Lebedev quadrature points) I want my plot to look similar to this one that I found online: 在Python的matplotlib中绘制球面上的点

我要画一个球面的图,在球面上画一些点。(特别是点是列别捷夫求积点)我想让我的图看起来和我在网上找到的图相似:

I proceed by plotting a spherical surface, and then overlaying it with a scatter plot. However, this results in most of my points being 'absorbed' by the underlying sphere, making them difficult to see. Take a look: 在Python的matplotlib中绘制球面上的点

我先画一个球面,然后用散点图叠加。然而,这导致我的大部分观点被下面的球体“吸收”,使它们很难被看到。看一看:

How can I prevent my points from being obscured by the sphere? Here is the script I use to generate this plot:

我怎样才能不让我的点被球体遮住呢?下面是我用来生成这个情节的脚本:

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

# Create a sphere
r = 1
pi = np.pi
cos = np.cos
sin = np.sin
phi, theta = np.mgrid[0.0:pi:100j, 0.0:2.0*pi:100j]
x = r*sin(phi)*cos(theta)
y = r*sin(phi)*sin(theta)
z = r*cos(phi)

#Import data
data = np.genfromtxt('leb.txt')
xx, yy, zz = np.hsplit(data, 3) 

#Set colours and render
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(
    x, y, z,  rstride=1, cstride=1, color='c', alpha=0.6, linewidth=0)

ax.scatter(xx,yy,zz,color="k",s=20)

ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
ax.set_zlim([-1,1])
ax.set_aspect("equal")
plt.tight_layout()
#plt.show()

Edit

I have found a way to do this using Python's mayavi. Here is what I get:

我已经找到了使用Python的mayavi实现这一点的方法。以下是我得到的:

在Python的matplotlib中绘制球面上的点

and here is the code I used:

这是我使用的代码:

from mayavi import mlab
import numpy as np

# Create a sphere
r = 1.0
pi = np.pi
cos = np.cos
sin = np.sin
phi, theta = np.mgrid[0:pi:101j, 0:2 * pi:101j]

x = r*sin(phi)*cos(theta)
y = r*sin(phi)*sin(theta)
z = r*cos(phi)

mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(400, 300))
mlab.clf()

data = np.genfromtxt('leb.txt')
xx, yy, zz = np.hsplit(data, 3)


mlab.mesh(x , y , z, color=(0.0,0.5,0.5))
mlab.points3d(xx, yy, zz, scale_factor=0.05)


mlab.show()

1 个解决方案

#1


9  

You can lower the alpha of the sphere if you think the points aren't showing up well enough. However, I think you may be processing the data into x, y, z coordinates incorrectly. I got a list of points from here: http://people.sc.fsu.edu/~jburkardt/m_src/sphere_lebedev_rule_display/sphere_lebedev_rule_display.html, and my sphere had points that looked kind of like yours until I realized that the file contained the values for theta and phi, and that I needed to turn degrees into radians.

如果你认为这些点表现得不够好,你可以降低球面的阿尔法值。但是,我认为您可能将数据处理成不正确的x、y、z坐标。我从这里得到了一个点列表:http://people.sc.fsu.edu/~jburkardt/m_src/sphere_lebedev_rule_display/sphere_lebedev_rule_display.html,我的球面上的点看起来有点像你的,直到我意识到这个文件包含了theta和phi的值,我需要把度转换成弧度。

Here's the code I used:

下面是我使用的代码:

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

# Create a sphere
r = 1
pi = np.pi
cos = np.cos
sin = np.sin
phi, theta = np.mgrid[0.0:pi:100j, 0.0:2.0*pi:100j]
x = r*sin(phi)*cos(theta)
y = r*sin(phi)*sin(theta)
z = r*cos(phi)

#Import data
data = np.genfromtxt('leb.txt')
theta, phi, r = np.hsplit(data, 3) 
theta = theta * pi / 180.0
phi = phi * pi / 180.0
xx = sin(phi)*cos(theta)
yy = sin(phi)*sin(theta)
zz = cos(phi)

#Set colours and render
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(
    x, y, z,  rstride=1, cstride=1, color='c', alpha=0.3, linewidth=0)

ax.scatter(xx,yy,zz,color="k",s=20)

ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
ax.set_zlim([-1,1])
ax.set_aspect("equal")
plt.tight_layout()
plt.show()

在Python的matplotlib中绘制球面上的点

#1


9  

You can lower the alpha of the sphere if you think the points aren't showing up well enough. However, I think you may be processing the data into x, y, z coordinates incorrectly. I got a list of points from here: http://people.sc.fsu.edu/~jburkardt/m_src/sphere_lebedev_rule_display/sphere_lebedev_rule_display.html, and my sphere had points that looked kind of like yours until I realized that the file contained the values for theta and phi, and that I needed to turn degrees into radians.

如果你认为这些点表现得不够好,你可以降低球面的阿尔法值。但是,我认为您可能将数据处理成不正确的x、y、z坐标。我从这里得到了一个点列表:http://people.sc.fsu.edu/~jburkardt/m_src/sphere_lebedev_rule_display/sphere_lebedev_rule_display.html,我的球面上的点看起来有点像你的,直到我意识到这个文件包含了theta和phi的值,我需要把度转换成弧度。

Here's the code I used:

下面是我使用的代码:

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

# Create a sphere
r = 1
pi = np.pi
cos = np.cos
sin = np.sin
phi, theta = np.mgrid[0.0:pi:100j, 0.0:2.0*pi:100j]
x = r*sin(phi)*cos(theta)
y = r*sin(phi)*sin(theta)
z = r*cos(phi)

#Import data
data = np.genfromtxt('leb.txt')
theta, phi, r = np.hsplit(data, 3) 
theta = theta * pi / 180.0
phi = phi * pi / 180.0
xx = sin(phi)*cos(theta)
yy = sin(phi)*sin(theta)
zz = cos(phi)

#Set colours and render
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(
    x, y, z,  rstride=1, cstride=1, color='c', alpha=0.3, linewidth=0)

ax.scatter(xx,yy,zz,color="k",s=20)

ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
ax.set_zlim([-1,1])
ax.set_aspect("equal")
plt.tight_layout()
plt.show()

在Python的matplotlib中绘制球面上的点