mplot3d:更改默认(相反)阴影
mplot3d: change default (opposite) shading
我正在使用 plot_surface
在 3D 中创建一个带有上下部分的圆锥形结构,请参见下面的代码。在视角 azim=90.
下,底部锥体左侧较亮,右侧较暗。如果 "light source" 来自左侧,人们会期望顶部锥体也是如此。然而,上锥体具有相反的阴影并且对于我选择的其他视角仍然如此。
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
),两个锥体看起来是均匀的,但我希望它们有一些(一致的)光照。提前致谢。
有趣的是,必须将底部圆锥体参数更改为 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.)
我正在使用 plot_surface
在 3D 中创建一个带有上下部分的圆锥形结构,请参见下面的代码。在视角 azim=90.
下,底部锥体左侧较亮,右侧较暗。如果 "light source" 来自左侧,人们会期望顶部锥体也是如此。然而,上锥体具有相反的阴影并且对于我选择的其他视角仍然如此。
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
),两个锥体看起来是均匀的,但我希望它们有一些(一致的)光照。提前致谢。
有趣的是,必须将底部圆锥体参数更改为 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.)