在matplotlib中抬起灯
我有以下python代码:
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams['figure.figsize'] = [12, 7]
n = 100
m = 100
X = np.arange(-n/2,n/2,1)
Y = np.arange(-m/2,m/2,1)
X, Y = np.meshgrid(X, Y)
landscape = np.exp(-0.01 * (X*X + Y*Y) )
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(X, Y, landscape,
linewidth=0,
antialiased=False
)
在笔记本中运行此图像
如果您看起来非常近距离,您会看到高斯峰的左侧比右手轻一些边。但是,这种照明效果几乎看不见,我想增加它,以使3D形状很容易看到。
我知道 matplotlib.colors.colors.lightsource ,但是无论我做什么,我都无法得到我想要的效果。理想情况下,我只是想增加“默认”照明的强度,而不是解决这个问题。但是,如果有人可以解释如何将LightsOrce用于此图像也将有所帮助。
请注意,我不想将高度图应用于图像,也不想在其上绘制网格线 - 我只想在保持表面均匀的颜色的同时增加照明效果。
还值得一提的是,我有点困扰matplotlib,因为我使用 jupyterlite 要与未安装Python的学生分享笔记本,因此我需要一个解决方案的解决方案。
I have the following Python code:
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams['figure.figsize'] = [12, 7]
n = 100
m = 100
X = np.arange(-n/2,n/2,1)
Y = np.arange(-m/2,m/2,1)
X, Y = np.meshgrid(X, Y)
landscape = np.exp(-0.01 * (X*X + Y*Y) )
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(X, Y, landscape,
linewidth=0,
antialiased=False
)
Running this in a notebook produces this image
If you look very closely you will see that the left-hand side of the Gaussian peak is very slightly lighter than the right-hand side. This lighting effect is barely visible, though, and I would like to increase it, so that the 3D shape becomes easily visible.
I'm aware of matplotlib.colors.LightSource, but no matter what I do I can't get that to produce the effect I want. Ideally, I'd just like to increase the intensity of the 'default' lighting, rather than fiddling around with this. But if someone can explain how to use LightSource for this image that would help too.
Note that I don't want to apply a height map to the image, and I also don't want to draw grid lines on it - I just want to increase the lighting effect while keeping the surface a uniform colour.
It's also worth mentioning that I'm somewhat stuck with MatPlotLib, because I'm using Jupyterlite to share the notebook with students who don't have Python installed, so I need a solution that works with that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
lightsource
您可以做如果要从右边的灯更改
azdeg
参数lightsource
CreationUsing
LightSource
you can do something likeIf you want the light from the right change the
azdeg
parameter at theLightSource
creation请记住,Matplotlib的3D功能相当有限,我建议使用其他库。 3D图的更好的库是:
请记住,这些图书馆中的每个图书馆都具有其优势和缺点。
我将使用Mayavi复制您的示例,因为我将其安装在当前环境中:
Keeping in mind that Matplotlib's 3D capabilities are rather limited, I would suggest to use a different library. Better libraries for 3D plots are:
Keep in mind that each one of those libraries come with their advantages and disadvantages.
I'm going to replicate your example with Mayavi, since I have it installed in the current environment: