简体   繁体   English

matplotlib:在2d图上投影3d曲面

[英]matplotlib : project 3d surface on 2d plot

Is there an equivalent to "Axes3DSubplot.plot_surface" in 2D ? 在2D中是否有等效于“ Axes3DSubplot.plot_surface”的文件? I am trying to plot the projection of a mesh on the XY-plane in matplotlib (so not in '3d' mode). 我正在尝试在matplotlib中绘制网格在XY平面上的投影(因此不在“ 3d”模式下)。

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. 但是在2D子图中。 I cannot find an equivalent for plot_surface in the doc of AxesSubplot. 我在AxesSubplot的文档中找不到plot_surface的等效项。 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 : 编辑:@Tim是的,我想在这种情况下,我设法做到了:

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. 另一个解决方案是使用matplotlib.patches.Polygon绘制每个投影面。

You can use contourf to produce coloured 2D contours: 您可以使用contourf生成彩色的2D轮廓:

# 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. 尽管这似乎并不是您真正需要的,但这是朝着正确方向迈出的一步。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM