【问题标题】:Pyplot: how to increase the resolution of plot_surface and how to remove the lines?Pyplot:如何提高 plot_surface 的分辨率以及如何去除线条?
【发布时间】:2021-07-20 07:56:56
【问题描述】:

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

考虑以下 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 向量的大小,但表面上还是可以看到线条,并且颜色分辨率很差:

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

【问题讨论】:

    标签: python matplotlib 3d surface


    【解决方案1】:

    根据 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 设置为 2D 数组的 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()
    

    【讨论】:

      猜你喜欢
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 2013-12-18
      • 2018-09-04
      • 2013-01-06
      相关资源
      最近更新 更多