【问题标题】:Matplotlib: plot triangles with colors associated to the pointsMatplotlib:绘制与点相关的颜色的三角形
【发布时间】:2021-02-07 04:23:13
【问题描述】:

我正在使用 Matplotlib 并且必须绘制一个三角形网格,其中的点在顶点处具有相关的颜色。网格必须是二维的,不是三维的,这是我见过的所有 trisurf 示例的规则。

x - x coordiantes of my points 
y - y coordiantes of my points 
s - values associated with my points (should be colors)
triangles - a list of indices [i,j,k] that indicates the triangles.

您能否提供一个示例,假设给定上述数据,生成三角形的二维网格(显示或不显示坐标轴)并根据顶点对三角形进行着色?如果线框仍然可见,那就太好了。

【问题讨论】:

    标签: matplotlib


    【解决方案1】:

    它被称为gouraud shading,并且可用 a.o.通过 matplotlib 的tripcolor()。在 matplotlib 中,仅支持将颜色作为给定颜色图中的值。它不是一个完整的 rgb 平滑。例如tricontourf() 使用它来插入颜色值。

    这是一个简单的例子。

    import matplotlib.pyplot as plt
    import matplotlib.tri as tri
    
    x = [1, 1, -1, -1, 0]
    y = [1, -1, -1, 1, 0]
    s = [0.1, 0.75, 0.0, 0.9, 1]
    triangles = [[4, 0, 1], [4, 1, 2], [4, 2, 3], [4, 3, 0]]
    triang = tri.Triangulation(x, y, triangles)
    
    cmap = plt.cm.rainbow
    fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 5))
    
    ax1.triplot(triang, lw=2, zorder=0) # draw the outlines of the triangles
    ax1.scatter(x, y, c=s, cmap=cmap, s=500) # show the colors of the points
    
    ax2.tripcolor(triang, s, cmap=cmap, shading='gouraud')
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      相关资源
      最近更新 更多