有效地绘制许多球体 [英] draw many spheres efficiently

查看:29
本文介绍了有效地绘制许多球体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一张图片中绘制许多小球和大球.以下代码有效,但运行时间非常长.

将 matplotlib.pyplot 导入为 plt从 mpl_toolkits.mplot3d 导入 Axes3D导入 numpyfig = plt.figure()ax = fig.gca(projection='3d')ax.set_aspect('相等')u = numpy.linspace(0, 2*numpy.pi, 100)v = numpy.linspace(0, numpy.pi, 100)x = numpy.outer(numpy.cos(u), numpy.sin(v))y = numpy.outer(numpy.sin(u), numpy.sin(v))z = numpy.outer(numpy.ones(numpy.size(u)), numpy.cos(v))对于范围内的 k(200):c = numpy.random.rand(3)r = numpy.random.rand(1)ax.plot_surface(r*x + c[0]、r*y + c[1]、r*z + c[2]、颜色='#1f77b4',阿尔法=0.5,线宽=0)plt.show()

我正在寻找更有效的解决方案.也许在 matplotlib 中有一个我没有找到的本地领域艺术家?

解决方案

不,没有领域艺术家"这样的东西.即使有,绘制它也不会花费更少的时间.

您在问题中提出的解决方案是绘制许多球体的明智方法.但是,您可能需要考虑在球体上使用更少的点,

u = numpy.linspace(0, 2*numpy.pi, 12)v = numpy.linspace(0, numpy.pi, 7)

一个应该始终考虑的选项是不使用 matplotlib 进行 3D 绘图,因为它是

I need to draw many spheres, small and large, in one picture. The following code works, but takes awfully long to run.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect('equal')

u = numpy.linspace(0, 2*numpy.pi, 100)
v = numpy.linspace(0, numpy.pi, 100)
x = numpy.outer(numpy.cos(u), numpy.sin(v))
y = numpy.outer(numpy.sin(u), numpy.sin(v))
z = numpy.outer(numpy.ones(numpy.size(u)), numpy.cos(v))

for k in range(200):
    c = numpy.random.rand(3)
    r = numpy.random.rand(1)
    ax.plot_surface(
        r*x + c[0], r*y + c[1], r*z + c[2],
        color='#1f77b4',
        alpha=0.5,
        linewidth=0
        )

plt.show()

I'm looking for a more efficient solution. Perhaps there is a native sphere artist in matplotlib that I didn't find?

解决方案

No, there is no such thing as a "sphere artist". And even if there was, it would not take less time to draw it.

The solution you present in the question is a sensible way to draw many spheres. However, you might want to consider using a lot less points on the sphere,

u = numpy.linspace(0, 2*numpy.pi, 12)
v = numpy.linspace(0, numpy.pi, 7)

An option one should always consider is not to use matplotlib for 3D plotting, as it is not actually been designed for it; and use Mayavi instead. The above in mayavi would look like

from mayavi import mlab
import numpy as np

[phi,theta] = np.mgrid[0:2*np.pi:12j,0:np.pi:12j]
x = np.cos(phi)*np.sin(theta)
y = np.sin(phi)*np.sin(theta)
z = np.cos(theta)

def plot_sphere(p):
    r,a,b,c = p
    return mlab.mesh(r*x+a, r*y+b, r*z+c)  


for k in range(200):
    c = np.random.rand(4)
    c[0] /= 10.
    plot_sphere(c)

mlab.show()

While the calculation takes a similar time, interactively zooming or panning is much faster in Mayavi.

Furthermore Mayavi actually provides something like a "sphere artist", which is called points3d

from mayavi import mlab
import numpy as np

c = np.random.rand(200,3)
r = np.random.rand(200)/10.

mlab.points3d(c[:,0],c[:,1],c[:,2],r)

mlab.show()

这篇关于有效地绘制许多球体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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