project 3d surface on 2d plot

Question:

Is there an equivalent to “Axes3DSubplot.plot_surface” in 2D ?
I am trying to plot the projection of a mesh on the XY-plane in matplotlib (so not in ‘3d’ mode).

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

# Data (wireframe sphere)
theta, phi = np.meshgrid( np.linspace(0, np.pi/2, 10), np.linspace(0, np.pi/2, 10) )

x = np.sin(theta) * np.cos(phi)
y = np.sin(theta) * np.sin(phi)
z = np.cos(theta)


fig = plt.figure()

# Subplot 3D
ax1 = fig.add_subplot(1, 2, 1, projection='3d', aspect='equal')

colors = matplotlib.cm.jet(np.hypot(x,y))

surface = ax1.plot_surface(x, y, z, rstride=1, cstride=1, facecolors = colors, alpha=0.5 )
projection = ax1.plot_surface(0, y, z, rstride=1, cstride=1, facecolors = colors )
projection.set_edgecolor('k')

# Subplot 2D
ax2 = fig.add_subplot(1, 2, 2, aspect='equal')

ax2.plot(y, z, 'k')
ax2.plot(y.T, z.T, 'k')

I am trying to produce a similar result than :

ax1.plot_surface(0, y, z, rstride=1, cstride=1, facecolors = colors )

But in the 2D subplot. I cannot find an equivalent for plot_surface in the doc of AxesSubplot. The only thing I managed to do is plot the wireframe (but not the facecolors) with :

ax2.plot(y, z, 'k')
ax2.plot(y.T, z.T, 'k')

I cannot upload an image, but basically, I want to put the “colors” in the second subplot.
Thanks,

EDIT:
@Tim
Yeah, I suppose, in this case, I managed to do it with :

ax2.contourf(y, z, np.hypot(x,y), levels=np.hypot(x,y)[0], cmap=matplotlib.cm.jet)

In a more generic case, you’ll need the right level-function and a bit of tweaking with the levels and colormap, but it seems doable.

Another solution would be to use matplotlib.patches.Polygon to draw each projected face.

Asked By: Nihl

||

Answers:

You can use contourf to produce coloured 2D contours:

# Subplot 2D
ax2 = fig.add_subplot(1, 2, 2, aspect='equal')
ax2.contourf(x, y, z, facecolors=colors)

Although this doesn’t seem to be exactly what you need, it’s a step in the right direction.

enter image description here

Answered By: Tim B
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.