微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pyplot:如何提高 plot_surface 的分辨率以及如何去除线条?

如何解决Pyplot:如何提高 plot_surface 的分辨率以及如何去除线条?

这似乎是一个很基础的问题,但是看了帮助功能,在网上搜索,还是没有找到解决办法。对不起,如果我在这里遗漏了一些明显的东西。

考虑遵循 MWE,该 MWE 旨在使用颜色图且不使用线条在极坐标中绘制 3D 图形:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

r=np.linspace(0,1,100)
theta=np.linspace(0,2*np.pi,10000)
R,Theta=np.meshgrid(r,theta)
X,Y=R*np.cos(Theta),R*np.sin(Theta)
Z=R*np.sin(Theta)*np.cos(Theta)

fig=plt.figure(1)
ax=fig.add_subplot(projection='3d')
ax.plot_surface(X,Y,Z,cmap=cm.inferno,linewidth=0)
plt.show()

如您在生成的图中所见,尽管询问了 linewidth=0 并夸大了 theta 向量的大小,但在表面上仍然可以看到线条,并且颜色分辨率很差:

enter image description here

如何去除白线,获得颜色不断变化的光滑表面?

解决方法

根据 Adam Murphy 的 this blog post 的说法,matplotlib 中的曲面图是通过填充线框图生成的,因此即使设置 linewidth=0 也很难完全消除线条伪影。

但是,查看 matplotlib's documentation on surface plots,有两个参数 rstridecstride 对 X、Y 和 Z 数组的行和列进行下采样 - 这两个参数的默认值参数是 10,所以只绘制每 10 行和 10 列。因此,如果我们将 rstride 和 cstride 降低到 5,那么您的曲面图应该具有更高的分辨率,尽管渲染速度会更慢。

为了提高渲染速度,文档建议将 rstride 和 cstride 设置为二维数组的 the number of rows - 1number of columns - 1 的倍数。由于 X、Y、Z 在您的原始代码中都有维度 (10000,100),我将这些数组的维度更改为 (10001,101) 以便 5 等分101-110001-1

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

## to maximize rendering speed,we want the parameters rstride and cstride 
## to be multiples of the number of rows-1 and columns-1
r=np.linspace(0,1,101)
theta=np.linspace(0,2*np.pi,10001)
R,Theta=np.meshgrid(r,theta)
X,Y=R*np.cos(Theta),R*np.sin(Theta)
Z=R*np.sin(Theta)*np.cos(Theta)

fig=plt.figure(1)
ax=fig.add_subplot(projection='3d')
ax.plot_surface(X,Y,Z,rstride=5,cstride=5,cmap=cm.inferno,linewidth=0)
plt.show()

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。