mcExchange
mcExchange

Reputation: 6494

Python: How to switch of outer axes in 3D subplots

I'm trying to plot two 3D plots side by side. However my result looks like this:

enter image description here

I'd like to remove the outer axes (the ones going from [0..1])

Everything I tried so far

ax.axis('off')

and

ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])

only removed inner axes (those of the 3D coordinate system), which I want to keep.

Here is my code so far:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Generate coordinates for scatter plot
x, y, z = np.meshgrid(np.arange(data1.shape[0]), np.arange(data1.shape[1]), np.arange(data1.shape[2]))
[x, y, z] = (np.reshape(x, (-1)), np.reshape(y, (-1)), np.reshape(z, (-1)))

# Generate scatter plot
numRows = 1
numCols = 2
fig, axes = plt.subplots(numRows, numCols, figsize=(20, 12))
ax = fig.add_subplot(121, projection='3d')
scat = ax.scatter(x, y, z, c=np.reshape(data1, (-1)), cmap='jet', marker="s")
ax = fig.add_subplot(122, projection='3d')
scat = ax.scatter(x, y, z, c=np.reshape(data2, (-1)), cmap='jet', marker="s")
plt.show()

Upvotes: 1

Views: 676

Answers (1)

DavidG
DavidG

Reputation: 25362

You are creating two sets of subplots. One by using

fig, axes = plt.subplots(...)

and the other by doing

ax = fig.add_subplot(121, projection='3d')

They pretty much do the same thing, which is why you are seeing two sets of axes. You don't need to create the two subplots when you are creating the figure. Instead do:

fig = plt.figure(figsize=(20, 12))

Full example:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(7, 5))

ax = fig.add_subplot(121, projection='3d')
ax = fig.add_subplot(122, projection='3d')

plt.show()

which gives:

enter image description here

Upvotes: 2

Related Questions