mplot3d:更改默认(相反)着色

时间:2017-10-03 18:22:57

标签: python matplotlib mplot3d

我正在使用plot_surface创建一个带有3D上下部分的圆锥形结构,请参阅下面的代码。对于视角azim=90.,底部锥体在左侧较浅而在右侧较暗。如果"光源"可以预期顶部锥体也是如此。来自左侧。然而,上锥体具有相反的阴影并且对于我选择的其他视角保持不变。

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(3,4));
ax = fig.add_subplot(111,projection='3d')

theta = np.linspace(0,2*np.pi,360)
r = np.linspace(0,1,100)
T, R = np.meshgrid(theta, r)

X = R * np.cos(T)
Y = R * np.sin(T)
Zup = np.sqrt(X**2 + Y**2)

ax.plot_surface(X, Y, Zup, rstride=1, cstride=1, linewidth=0, 
               antialiased=True,alpha=0.7,color='orange')
ax.plot_surface(X, Y, -Zup, rstride=1, cstride=1, linewidth=0, 
               antialiased=True,alpha=0.7,color='orange')

ax.set_axis_off()
ax.view_init(elev=4., azim=90.)
ax.dist=6

fig.tight_layout(pad=0.)

在某个角度(例如azim=45),两个锥体看起来都是同质的,但我希望它们有一些(一致的)光照。提前致谢。

enter image description here

1 个答案:

答案 0 :(得分:0)

有趣的是,必须将底部圆锥参数更改为X[::-1], Y[::-1], -Zup[::-1]以获得与顶部参数相同的阴影。完整代码如下。

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(2,2.6));
ax = fig.add_subplot(111,projection='3d')

theta = np.linspace(0,2*np.pi,360)
r = np.linspace(0,1,100)
T, R = np.meshgrid(theta, r)

X = R * np.cos(T)
Y = R * np.sin(T)
Zup = np.sqrt(X**2 + Y**2)

ax.plot_surface(X, Y, Zup, rstride=1, cstride=1, linewidth=0, 
antialiased=True,alpha=0.7,color='orange')
# fix here
# have to reverse the lists to get the same shading
ax.plot_surface(X[::-1], Y[::-1], -Zup[::-1], rstride=1, cstride=1, 
linewidth=0, antialiased=True,alpha=0.7,color='orange')

ax.set_axis_off()
ax.view_init(elev=4., azim=90.)
ax.dist=6

fig.tight_layout(pad=0.)

enter image description here