Bothering frame on matplotlib 3D plots

Viewed 201

I make 3d plots with matplotlib and I always get a weird frame with a normalized scale around my plot. Where does it come from and how can I get rid of it ?

Here is an example code that drives me to the problem :

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



x = np.linspace(0,10)
y = np.linspace(0,10)
z = np.linspace(0,10)

# ------------- Figure ---------------
fig, ax = plt.subplots(figsize = (9,6))

ax = fig.gca(projection='3d')
ax.plot(np.sin(x), np.cos(y), z)

plt.show()

And here is the result : enter image description here

I use plt.subplots() because I want a figure with a 3D and a 2D plot side by side.

1 Answers

You call plt.subplots(...) and this, of course, instantiates an Axes, complete of horizontal and vertical spines, before Matplotlib is informed that you want a 3D enabled Axes.

When you later call plt.gca(...) it's too late…

Simply use

fig, ax = plt.subplots(figsize = (9,6), subplot_kw={"projection" : "3d"})

or

fig = plt.figure(figsize = (9,6))
ax = fig.add_subplot(111, projection='3d')

Addressing OP's comment

enter image description here

Figure.add_subplot is pretty flexible…

fig = plt.figure()
fig.add_subplot(1,5,(1,4), projection='3d')
fig.add_subplot(1,5,5)
fig.tight_layout()
plt.show()
Related