【发布时间】:2015-02-09 19:48:39
【问题描述】:
我之前曾问过类似的问题 (How to set a fixed/static size of circle marker on a scatter plot?),但现在我想在 3D 中进行。我怎样才能做到这一点?
谢谢
【问题讨论】:
标签: matplotlib 3d marker scatter
我之前曾问过类似的问题 (How to set a fixed/static size of circle marker on a scatter plot?),但现在我想在 3D 中进行。我怎样才能做到这一点?
谢谢
【问题讨论】:
标签: matplotlib 3d marker scatter
在 2D 情况下,您需要自己绘制球体。如果您想要形状优美的球体,这意味着要绘制许多补丁,因此会很快变慢。
这是一个基本的方法:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
def plot_shere(ax, x, y, z, r, resolution=100, **kwargs):
""" simple function to plot a sphere to a 3d axes """
u = np.linspace(0, 2 * np.pi, resolution)
v = np.linspace(0, np.pi, resolution)
xx = r * np.outer(np.cos(u), np.sin(v)) + x
yy = r * np.outer(np.sin(u), np.sin(v)) + y
zz = r * np.outer(np.ones(np.size(u)), np.cos(v)) + z
ax.plot_surface(xx, yy, zz, rstride=4, cstride=4, **kwargs)
# create some random data (set seed to make it reproducable)
np.random.seed(0)
(x,y,z) = np.random.randint(0,10,(3,5))
r = np.random.randint(2,4,(5,))
# set up the figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# loop through the data and plot the spheres
for p in zip(x,y,z,r):
plot_shere(ax, *p, edgecolor='none', color=np.random.rand(3))
# set the axes limits and show the plot
ax.set_ylim([-4,14])
ax.set_xlim([-4,14])
ax.set_zlim([-4,14])
plt.show()
结果:
【讨论】: