如何使用matplotlib在2D磁盘旁边绘制3D球体

时间:2019-06-28 08:17:34

标签: python matplotlib scatter-plot

我在球体内有坐标,我想将整个3D球体绘制为散点,但在旁边我还想绘制2D磁盘。

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font_scale=1)
sns.set_style("whitegrid")
from matplotlib import rc
rc('text', usetex=True)
rc('font', family='serif')
from mpl_toolkits.mplot3d import Axes3D

fig, (ax1, ax3) = plt.subplots(1, 2, figsize=(900 / 106, 600 / 106), constrained_layout = False)

# 2D
radius = np.random.uniform(size=5000)
phi = np.random.uniform(size=5000) * 2 * np.pi
x = radius * np.cos(phi)
y = radius * np.sin(phi)

plot = ax1.scatter(x, y, s = 10, marker='.')
ax1.set_xlabel('$x$')
ax1.set_ylabel('$y$')
ax1.set_aspect('equal')

# 2D
radius = np.random.uniform(size=5000)
phi = np.random.uniform(size=5000) * 2 * np.pi
alfa = np.random.uniform(size=5000) * np.pi
x = radius * np.cos(phi) * np.sin(alfa)
y = radius * np.sin(phi) * np.sin(alfa)
z = radius * np.cos(alfa)

ax3 = Axes3D(fig)
plot = ax3.scatter(x, y, z, s = 1, marker='.')
ax3.set_xlabel('$x$')
ax3.set_ylabel('$y$')
ax3.set_zlabel('$z$')
#ax3.view_init(30, 240)
ax3.set_aspect('equal', 'box')
#fig.colorbar(plot, shrink = 0.9, ticks = np.linspace(0, 1, 6), ax = ax3)


fig.tight_layout()

现在,上面这段代码的问题在于输出结果与我的预期不尽相同(3D绘制在顶部)。

enter image description here

任何想法如何正确执行此操作?

0 个答案:

没有答案