用于闪亮的Python 3D表面图的Phong着色

时间:2015-01-30 09:32:16

标签: python matlab matplotlib mayavi phong

我尝试使用镜面着色在Python中创建美学上令人愉悦的3D绘图,并且迄今为止尝试使用Matplotlib与3D轴和Mayavi的表面绘图,例如,来自Mayavi冲浪示例网页:

enter image description here

结果看起来不错,而在Mayavi,似乎有合理的照明控制,虽然我似乎无法实现一个闪亮的光线。外观

在Matlab中,这可以通过使用Phong'来实现。照明:

enter image description here

请参阅http://www.mathworks.com/matlabcentral/fileexchange/35240-matlab-plot-gallery-change-lighting-to-phong/content/html/Lighting_Phong.html

因此,我的问题是:如何在基于Python的3D绘图中实现这种Phong风格的闪亮阴影?

2 个答案:

答案 0 :(得分:10)

As @ben mentioned, you can use Mayavi and then interactively change the lighting. A good idea is to click in the record script button, then you can use those lines of code in your scripts (That's how I did for the Mayavi part here).

Another option is to use Matplotlib. Based on the shading example, I managed to generate a surface with lighting.

See the code below.

import numpy as np
from mayavi import mlab
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import LightSource

## Test data: Matlab `peaks()`
x, y = np.mgrid[-3:3:150j,-3:3:150j]
z =  3*(1 - x)**2 * np.exp(-x**2 - (y + 1)**2) \
   - 10*(x/5 - x**3 - y**5)*np.exp(-x**2 - y**2) \
   - 1./3*np.exp(-(x + 1)**2 - y**2) 

## Mayavi
surf = mlab.surf(x, y, z, colormap='RdYlBu', warp_scale='auto')
# Change the visualization parameters.
surf.actor.property.interpolation = 'phong'
surf.actor.property.specular = 0.1
surf.actor.property.specular_power = 5



## Matplotlib
fig = plt.figure()
ax = fig.gca(projection='3d')

# Create light source object.
ls = LightSource(azdeg=0, altdeg=65)
# Shade data, creating an rgb array.
rgb = ls.shade(z, plt.cm.RdYlBu)
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, linewidth=0,
                       antialiased=False, facecolors=rgb)
plt.show()
mlab.show()

This gives as results:

  • Mayavi: enter image description here
  • Matplotlib: enter image description here

答案 1 :(得分:2)

是的,你可以在Mayavi做到这一点。在Mayavi窗口中,单击左上角的小Mayavi图标以显示高级菜单。单击场景中与曲面对应的曲面,然后单击右侧菜单中的“Actor”选项卡,向下滚动到标有“Property”的框,然后单击“More options”。您可以在“插值”框中将阴影设置为phong着色。