在matplotlib中绘制一个曲面(球体),以0,0,0以外的坐标为中心

时间:2016-12-05 23:19:29

标签: python matplotlib plot graph geometry

我需要绘制透明度,半径和x,y,z坐标不同的球体。

我可以绘制球体并输入透明度(alpha)值和半径值(我已将其改为随机数以简化此示例),但我还没有能够计算出来如何将球体围绕坐标,而不是0,0,0。

如何使用pythons matplotlib libary将球体围绕0,0,0以外的坐标居中?

这是我到目前为止所拥有的;

from __future__ import print_function
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import time , random

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim3d([100.0, 0.0])
ax.set_xlabel('X')
ax.set_ylim3d([0.0, 100.0])
ax.set_ylabel('Y')
ax.set_zlim3d([0.0, 100.0])
ax.set_zlabel('Z')
ax.set_title('pFC-amygdala-insula')

wframe = None
tstart = time.time()
for num in range(100):
    oldcol = wframe
    r = random.randint(1, 20)
    alpha = 1.0 / random.randint(25, 100)
    u = np.linspace(0, 2 * np.pi, 100)
    v = np.linspace(0, np.pi, 100)
    x = r * np.outer(np.cos(u), np.sin(v))
    y = r * np.outer(np.sin(u), np.sin(v))
    z = r * np.outer(np.ones(np.size(u)), np.cos(v))
    sphere = ax.plot_surface(x, y, z, color='b', alpha=alpha)
    if oldcol is not None: ax.collections.remove(oldcol)
    plt.pause(.001)

print('Duration: %f' % (100 / (time.time() - tstart)))

1 个答案:

答案 0 :(得分:1)

只需将中心的坐标添加到x,y和z。

x = r * np.outer(np.cos(u), np.sin(v)) + center_x
y = r * np.outer(np.sin(u), np.sin(v)) + center_y
z = r * np.outer(np.ones(np.size(u)), np.cos(v)) + center_z