Yair Daon
Yair Daon

Reputation: 1123

Two different graphs in the same plot

I want to plot a wirerframe and a scatter plot in the same plot. Here's what I do:

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

fig = plt.figure()

ax1 = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax1.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

ax2 = fig.add_subplot(111, projection='3d')
xs = np.array( [ 1, 0 ,2 ])
ys = np.array( [ 1, 0, 2 ])
zs = np.array( [ 1, 2, 3 ])
ax2.scatter(xs, ys, zs)

plt.show()

This script just gives the scatter plot. Comment any block and you get the uncommented plot. But they won't go on the same plot together.

Upvotes: 1

Views: 2346

Answers (1)

Danica
Danica

Reputation: 28846

When you add_subplot(111) again, you override the previous subplot. Just don't do that, and plot on the same axes twice:

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

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

xs = np.array( [ 1, 0 ,2 ])
ys = np.array( [ 1, 0, 2 ])
zs = np.array( [ 1, 2, 3 ])
ax.scatter(xs, ys, zs)

plt.show()

Upvotes: 3

Related Questions