2
我需要在一張圖片中繪製許多球體,大小不一。以下代碼有效,但需要很長時間才能運行。高效地繪製多個球體
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()
我正在尋找一個更有效的解決方案。也許在matplotlib中有一個本地球體藝術家,我沒有找到?