Phong着色可生成闪亮的Python 3D表面图 [英] Phong shading for shiny Python 3D surface plots

查看:318
本文介绍了Phong着色可生成闪亮的Python 3D表面图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用镜面反射阴影在Python中创建美观的3D绘图,到目前为止,我尝试同时使用具有3D轴的Matplotlib和Mayavi(例如Mayavi冲浪示例网页)中的曲面图:

I'm trying to create aesthetically pleasing 3D plots in Python with specular shading, and thus far have tried using both Matplotlib with 3D axes and surface plots from Mayavi, e.g., from the Mayavi surf examples web page:

结果看起来不错,在Mayavi中,似乎确实可以合理地控制照明,尽管我似乎无法获得闪亮"的外观.

The results look good, and in Mayavi there does seem to be reasonable control of the lighting, although I can't seem to achieve a "shiny" appearance.

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

In Matlab, this can be achieved by using 'Phong' lighting:

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

Therefore, my question is: how can I achieve this Phong-style shiny shading in a Python-based 3D plot?

推荐答案

如@ben所述,您可以使用 Mayavi ,然后以交互方式更改照明.一个好主意是单击记录脚本按钮,然后可以在脚本中使用这些代码行(这就是我在此处的Mayavi部分所做的事情).

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).

另一种选择是使用 Matplotlib .根据着色示例,我设法生成了一个带有照明的表面.

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

请参见下面的代码.

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()

作为结果:

  • Mayavi:
  • Matplotlib:
  • Mayavi:
  • Matplotlib:

这篇关于Phong着色可生成闪亮的Python 3D表面图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆