在球面上绘制点

27

我正在尝试生成一个球体的图形,并在球体表面绘制一些点。(具体来说,这些点是Lebedev积分点) 我希望我的图像看起来与我在网上找到的这个类似:enter image description here

我首先绘制了一个球面,然后将其与散点图叠加。然而,这导致大多数点被底层球体“吸收”,使它们难以看清。看看这张图:enter image description here

如何防止我的点被球体遮挡? 这是我用于生成此图的脚本:

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

编辑

我已经找到了使用Python的mayavi来实现这个的方法。下面是我的代码:

enter image description here

这是我使用的代码:

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

2
很遗憾,我不确定这是否容易实现,“mplot3d”在深度感知和分层方面表现不佳(这里没有真正的z缓冲区)。您将不得不使用MayaVI(您有Python = <2.7)或VisPy。 - Yann
你认为在Gnuplot中可能吗? - O Smith
2
我认为你应该将你的答案发布为一个真正的答案,而不是编辑你的问题并自我接受它(请参见http://stackoverflow.com/help/self-answer)。非常好的帖子,感谢分享。 - Jean-Sébastien
2个回答

21

如果您认为点没有显示得足够清晰,可以降低球体的alpha值。然而,我认为您可能将数据处理成了错误的x、y、z坐标。我从这里获得了一个点列表:http://people.sc.fsu.edu/~jburkardt/m_src/sphere_lebedev_rule_display/sphere_lebedev_rule_display.html,我的球体看起来有点像您的,直到我意识到该文件包含了theta和phi的值,并且我需要将度数转换为弧度。

lebedev_071.txt,1730个点规则,精度71。

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

Spherical graph

lebedev_025.txt, 230点规则,精度25。

   0.000000000000000    90.000000000000000    -0.055226399197273
 180.000000000000000    90.000000000000000    -0.055226399197273
  90.000000000000000    90.000000000000000    -0.055226399197273
 -90.000000000000000    90.000000000000000    -0.055226399197273
  90.000000000000000     0.000000000000000    -0.055226399197273
  90.000000000000000   180.000000000000000    -0.055226399197273
  45.000000000000000    54.735610317245346     0.004450274607445
  45.000000000000000   125.264389682754654     0.004450274607445
 -45.000000000000000    54.735610317245346     0.004450274607445
 -45.000000000000000   125.264389682754654     0.004450274607445
 135.000000000000000    54.735610317245346     0.004450274607445
 135.000000000000000   125.264389682754654     0.004450274607445
-135.000000000000000    54.735610317245346     0.004450274607445
-135.000000000000000   125.264389682754654     0.004450274607445
  45.000000000000000    39.440090784780402     0.004496841067921
  45.000000000000000   140.559909215219591     0.004496841067921
 -45.000000000000000    39.440090784780402     0.004496841067921
 -45.000000000000000   140.559909215219591     0.004496841067921
 135.000000000000000    39.440090784780402     0.004496841067921
 135.000000000000000   140.559909215219591     0.004496841067921
-135.000000000000000    39.440090784780402     0.004496841067921
-135.000000000000000   140.559909215219591     0.004496841067921
  59.815442273124063    63.307345060625650     0.004496841067921
 -59.815442273124063    63.307345060625650     0.004496841067921
  59.815442273124063   116.692654939374364     0.004496841067921
 -59.815442273124063   116.692654939374364     0.004496841067921
 120.184557726875937    63.307345060625650     0.004496841067921
-120.184557726875937    63.307345060625650     0.004496841067921
 120.184557726875937   116.692654939374364     0.004496841067921
-120.184557726875937   116.692654939374364     0.004496841067921
  30.184557726875941    63.307345060625650     0.004496841067921
 149.815442273124063    63.307345060625650     0.004496841067921
  30.184557726875941   116.692654939374364     0.004496841067921
 149.815442273124063   116.692654939374364     0.004496841067921
 -30.184557726875941    63.307345060625650     0.004496841067921
-149.815442273124063    63.307345060625650     0.004496841067921
 -30.184557726875941   116.692654939374364     0.004496841067921
-149.815442273124063   116.692654939374364     0.004496841067921
  45.000000000000000    20.881794557261646     0.005049153450479
  45.000000000000000   159.118205442738343     0.005049153450479
 -45.000000000000000    20.881794557261646     0.005049153450479
 -45.000000000000000   159.118205442738343     0.005049153450479
 135.000000000000000    20.881794557261646     0.005049153450479
 135.000000000000000   159.118205442738343     0.005049153450479
-135.000000000000000    20.881794557261646     0.005049153450479
-135.000000000000000   159.118205442738343     0.005049153450479
  74.903220296612005    75.401622829462283     0.005049153450479
 -74.903220296612005    75.401622829462283     0.005049153450479
  74.903220296612005   104.598377170537717     0.005049153450479
 -74.903220296612005   104.598377170537717     0.005049153450479
 105.096779703387995    75.401622829462283     0.005049153450479
-105.096779703387995    75.401622829462283     0.005049153450479
 105.096779703387995   104.598377170537717     0.005049153450479
-105.096779703387995   104.598377170537717     0.005049153450479
  15.096779703387996    75.401622829462283     0.005049153450479
 164.903220296612034    75.401622829462283     0.005049153450479
  15.096779703387996   104.598377170537717     0.005049153450479
 164.903220296612034   104.598377170537717     0.005049153450479
 -15.096779703387996    75.401622829462283     0.005049153450479
-164.903220296612034    75.401622829462283     0.005049153450479
 -15.096779703387996   104.598377170537717     0.005049153450479
-164.903220296612034   104.598377170537717     0.005049153450479
  45.000000000000000    80.891636123006165     0.003976408018052
  45.000000000000000    99.108363876993835     0.003976408018052
 -45.000000000000000    80.891636123006165     0.003976408018052
 -45.000000000000000    99.108363876993835     0.003976408018052
 135.000000000000000    80.891636123006165     0.003976408018052
 135.000000000000000    99.108363876993835     0.003976408018052
-135.000000000000000    80.891636123006165     0.003976408018052
-135.000000000000000    99.108363876993835     0.003976408018052
  12.774805990014807    45.717979481517574     0.003976408018052
 -12.774805990014807    45.717979481517574     0.003976408018052
  12.774805990014807   134.282020518482426     0.003976408018052
 -12.774805990014807   134.282020518482426     0.003976408018052
 167.225194009985188    45.717979481517574     0.003976408018052
-167.225194009985188    45.717979481517574     0.003976408018052
 167.225194009985188   134.282020518482426     0.003976408018052
-167.225194009985188   134.282020518482426     0.003976408018052
  77.225194009985188    45.717979481517574     0.003976408018052
 102.774805990014812    45.717979481517574     0.003976408018052
  77.225194009985188   134.282020518482426     0.003976408018052
 102.774805990014812   134.282020518482426     0.003976408018052
 -77.225194009985188    45.717979481517574     0.003976408018052
-102.774805990014812    45.717979481517574     0.003976408018052
 -77.225194009985188   134.282020518482426     0.003976408018052
-102.774805990014812   134.282020518482426     0.003976408018052
  45.000000000000000    68.685581154790029     0.004401400650381
  45.000000000000000   111.314418845209985     0.004401400650381
 -45.000000000000000    68.685581154790029     0.004401400650381
 -45.000000000000000   111.314418845209985     0.004401400650381
 135.000000000000000    68.685581154790029     0.004401400650381
 135.000000000000000   111.314418845209985     0.004401400650381
-135.000000000000000    68.685581154790029     0.004401400650381
-135.000000000000000   111.314418845209985     0.004401400650381
  28.889424740291254    48.796111385350962     0.004401400650381
 -28.889424740291254    48.796111385350962     0.004401400650381
  28.889424740291254   131.203888614649060     0.004401400650381
 -28.889424740291254   131.203888614649060     0.004401400650381
 151.110575259708753    48.796111385350962     0.004401400650381
-151.110575259708753    48.796111385350962     0.004401400650381
 151.110575259708753   131.203888614649060     0.004401400650381
-151.110575259708753   131.203888614649060     0.004401400650381
  61.110575259708753    48.796111385350962     0.004401400650381
 118.889424740291247    48.796111385350962     0.004401400650381
  61.110575259708753   131.203888614649060     0.004401400650381
 118.889424740291247   131.203888614649060     0.004401400650381
 -61.110575259708753    48.796111385350962     0.004401400650381
-118.889424740291247    48.796111385350962     0.004401400650381
 -61.110575259708753   131.203888614649060     0.004401400650381
-118.889424740291247   131.203888614649060     0.004401400650381
  45.000000000000000     3.274152069216487     0.017245443505444
  45.000000000000000   176.725847930783516     0.017245443505444
 -45.000000000000000     3.274152069216487     0.017245443505444
 -45.000000000000000   176.725847930783516     0.017245443505444
 135.000000000000000     3.274152069216487     0.017245443505444
 135.000000000000000   176.725847930783516     0.017245443505444
-135.000000000000000     3.274152069216487     0.017245443505444
-135.000000000000000   176.725847930783516     0.017245443505444
  87.683564415961172    87.685455250362111     0.017245443505444
 -87.683564415961172    87.685455250362111     0.017245443505444
  87.683564415961172    92.314544749637903     0.017245443505444
 -87.683564415961172    92.314544749637903     0.017245443505444
  92.316435584038842    87.685455250362111     0.017245443505444
 -92.316435584038842    87.685455250362111     0.017245443505444
  92.316435584038842    92.314544749637903     0.017245443505444
 -92.316435584038842    92.314544749637903     0.017245443505444
   2.316435584038771    87.685455250362111     0.017245443505444
 177.683564415961257    87.685455250362111     0.017245443505444
   2.316435584038771    92.314544749637903     0.017245443505444
 177.683564415961257    92.314544749637903     0.017245443505444
  -2.316435584038771    87.685455250362111     0.017245443505444
-177.683564415961257    87.685455250362111     0.017245443505444
  -2.316435584038771    92.314544749637903     0.017245443505444
-177.683564415961257    92.314544749637903     0.017245443505444
  54.381587934584054    90.000000000000000     0.004231083095357
 -54.381587934584054    90.000000000000000     0.004231083095357
 125.618412065415953    90.000000000000000     0.004231083095357
-125.618412065415953    90.000000000000000     0.004231083095357
  35.618412065415953    90.000000000000000     0.004231083095357
 -35.618412065415953    90.000000000000000     0.004231083095357
 144.381587934584047    90.000000000000000     0.004231083095357
-144.381587934584047    90.000000000000000     0.004231083095357
   0.000000000000000    35.618412065415953     0.004231083095357
   0.000000000000000   144.381587934584047     0.004231083095357
 180.000000000000000    35.618412065415953     0.004231083095357
 180.000000000000000   144.381587934584047     0.004231083095357
   0.000000000000000    54.381587934584054     0.004231083095357
   0.000000000000000   125.618412065415953     0.004231083095357
 180.000000000000000    54.381587934584054     0.004231083095357
 180.000000000000000   125.618412065415953     0.004231083095357
  90.000000000000000    35.618412065415953     0.004231083095357
  90.000000000000000   144.381587934584047     0.004231083095357
 -90.000000000000000    35.618412065415953     0.004231083095357
 -90.000000000000000   144.381587934584047     0.004231083095357
  90.000000000000000    54.381587934584054     0.004231083095357
  90.000000000000000   125.618412065415953     0.004231083095357
 -90.000000000000000    54.381587934584054     0.004231083095357
 -90.000000000000000   125.618412065415953     0.004231083095357
  69.231820019013028    90.000000000000000     0.005198069864064
 -69.231820019013028    90.000000000000000     0.005198069864064
 110.768179980986986    90.000000000000000     0.005198069864064
-110.768179980986986    90.000000000000000     0.005198069864064
  20.768179980986979    90.000000000000000     0.005198069864064
 -20.768179980986979    90.000000000000000     0.005198069864064
 159.231820019013014    90.000000000000000     0.005198069864064
-159.231820019013014    90.000000000000000     0.005198069864064
   0.000000000000000    20.768179980986979     0.005198069864064
   0.000000000000000   159.231820019013014     0.005198069864064
 180.000000000000000    20.768179980986979     0.005198069864064
 180.000000000000000   159.231820019013014     0.005198069864064
   0.000000000000000    69.231820019013028     0.005198069864064
   0.000000000000000   110.768179980986986     0.005198069864064
 180.000000000000000    69.231820019013028     0.005198069864064
 180.000000000000000   110.768179980986986     0.005198069864064
  90.000000000000000    20.768179980986979     0.005198069864064
  90.000000000000000   159.231820019013014     0.005198069864064
 -90.000000000000000    20.768179980986979     0.005198069864064
 -90.000000000000000   159.231820019013014     0.005198069864064
  90.000000000000000    69.231820019013028     0.005198069864064
  90.000000000000000   110.768179980986986     0.005198069864064
 -90.000000000000000    69.231820019013028     0.005198069864064
 -90.000000000000000   110.768179980986986     0.005198069864064
  64.963704081332708    32.473856655655446     0.004695720972569
  64.963704081332708   147.526143344344547     0.004695720972569
 -64.963704081332708    32.473856655655446     0.004695720972569
 -64.963704081332708   147.526143344344547     0.004695720972569
 115.036295918667292    32.473856655655446     0.004695720972569
 115.036295918667292   147.526143344344547     0.004695720972569
-115.036295918667292    32.473856655655446     0.004695720972569
-115.036295918667292   147.526143344344547     0.004695720972569
  74.926112157973748    60.891424466952714     0.004695720972569
  74.926112157973748   119.108575533047286     0.004695720972569
 -74.926112157973748    60.891424466952714     0.004695720972569
 -74.926112157973748   119.108575533047286     0.004695720972569
 105.073887842026252    60.891424466952714     0.004695720972569
 105.073887842026252   119.108575533047286     0.004695720972569
-105.073887842026252    60.891424466952714     0.004695720972569
-105.073887842026252   119.108575533047286     0.004695720972569
  25.036295918667289    32.473856655655446     0.004695720972569
  25.036295918667289   147.526143344344547     0.004695720972569
 -25.036295918667289    32.473856655655446     0.004695720972569
 -25.036295918667289   147.526143344344547     0.004695720972569
 154.963704081332708    32.473856655655446     0.004695720972569
 154.963704081332708   147.526143344344547     0.004695720972569
-154.963704081332708    32.473856655655446     0.004695720972569
-154.963704081332708   147.526143344344547     0.004695720972569
  60.030959593932515    76.866650451671518     0.004695720972569
  60.030959593932515   103.133349548328482     0.004695720972569
 -60.030959593932515    76.866650451671518     0.004695720972569
 -60.030959593932515   103.133349548328482     0.004695720972569
 119.969040406067492    76.866650451671518     0.004695720972569
 119.969040406067492   103.133349548328482     0.004695720972569
-119.969040406067492    76.866650451671518     0.004695720972569
-119.969040406067492   103.133349548328482     0.004695720972569
  15.073887842026251    60.891424466952714     0.004695720972569
  15.073887842026251   119.108575533047286     0.004695720972569
 -15.073887842026251    60.891424466952714     0.004695720972569
 -15.073887842026251   119.108575533047286     0.004695720972569
 164.926112157973762    60.891424466952714     0.004695720972569
 164.926112157973762   119.108575533047286     0.004695720972569
-164.926112157973762    60.891424466952714     0.004695720972569
-164.926112157973762   119.108575533047286     0.004695720972569
  29.969040406067499    76.866650451671518     0.004695720972569
  29.969040406067499   103.133349548328482     0.004695720972569
 -29.969040406067499    76.866650451671518     0.004695720972569
 -29.969040406067499   103.133349548328482     0.004695720972569
 150.030959593932522    76.866650451671518     0.004695720972569
 150.030959593932522   103.133349548328482     0.004695720972569
-150.030959593932522    76.866650451671518     0.004695720972569
-150.030959593932522   103.133349548328482     0.004695720972569

谢谢,看起来很不错!但是我必须说,我使用的数据不像你的极坐标那样 - 它已经是笛卡尔坐标系下的x、y、z。我已经找到了另一个解决方案来解决我的问题 - 我会编辑我的帖子来包含它 :) - O Smith
2
针对较新版本的matplotlib进行更新;使用ax.set_box_aspect((1,1,1))代替ax.set_aspect("equal")以获得等比例轴。 - ELNJ

1

尝试使用zorder参数。在下面给出的示例中,3D线图将显示在3D trisurf图层的顶部。为什么zorder从0到10而不是从0到1,请参见这里

plt_axes.plot_trisurf(x, y, z, shade=False, color='blue', cmap='Blues', zorder=0)
plt_axes.plot(x, y, z, marker='.', linestyle='None', label='Label', color='red', zorder=10)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接