0

I plan to create a figure in matplotlib, with a 3D surface on the left and its corresponding contour map on the right.

I used subplots but it only show the contour map (with blank space for the surface), and a separate figure for the surface.

Is it possible to create these plots in one figure side-by side?

EDIT: The code is as follows:

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

x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x**2 + y**2)
z = np.sin(r)

fig, (surf, cmap) = plt.subplots(1, 2)

fig = plt.figure()
surf = fig.gca(projection='3d')

surf.plot_surface(x,y,z)

cmap.contourf(x,y,z,25)

plt.show()
jjv
  • 167
  • 1
  • 5
  • 16

1 Answers1

1

I guess it's hard to use plt.subplots() in order to create a grid of plots with different projections.

So the most straight forward solution is to create each subplot individually with plt.subplot.

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

x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x**2 + y**2)
z = np.sin(r)

ax = plt.subplot(121, projection='3d')
ax.plot_surface(x,y,z)

ax2 = plt.subplot(122)
ax2.contourf(x,y,z,25)

plt.show()

Of course one may also use the gridspec capabilities for more sophisticated grid structures.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712