0

I have coordinates inside a sphere and I want to plot the whole 3D sphere as scattered points, but alongside I would also like to plot a 2D disk.

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()

Now the problem with this code above is that the output is not nearly as what I would expect (the 3D is plotted on top).

enter image description here

Any ideas how to do this properly?

skrat
  • 648
  • 2
  • 10
  • 27
  • 1
    `ax3 = Axes3D(fig)` will create a new axes instead of your subplot. You have to use `fig.add_subplot(122, projection='3d')` to create the axes separately. And `(121)` for the 2d axes. – Andras Deak -- Слава Україні Jun 28 '19 at 08:20
  • The version you're using seems to be written for ancient matplotlib. See [this duplicate](https://stackoverflow.com/questions/8510678/trying-to-add-a-3d-subplot-to-a-matplotlib-figure). Another duplicate: https://stackoverflow.com/questions/39616547/python-multiple-3d-plots-in-one-window – Andras Deak -- Слава Україні Jun 28 '19 at 08:28

0 Answers0