【问题标题】:something like plt.matshow but with triangles类似 plt.matshow 但带有三角形的东西
【发布时间】:2017-11-23 19:14:31
【问题描述】:

基本上,我想做类似下面的东西(三角形而不是正方形,通常用于 plt.matshow)。

可以从四个二维数组开始,每个数组代表一组三角形的颜色值:右、左、下、上:

import numpy as np
right=np.random.randn(8, 8)
left=np.random.randn(8, 8)
bottom=np.random.randn(8, 8)
top=np.random.randn(8, 8)

但我不知道剧情...

【问题讨论】:

标签: python matplotlib


【解决方案1】:

请参阅 matplotlib 文档 here 中的示例 matplotlib.pyplot.tripcolor(*args, **kwargs)。 这是你需要的简单版本:

import matplotlib.pyplot as plt
import numpy as np

xy = np.asarray([
    [-0.01, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890]])

x = xy[:, 0]*180/3.14159
y = xy[:, 1]*180/3.14159

triangles = np.asarray([[3, 2,  0]  , [3,  1, 2],   [ 0, 2,  1] , 
                        [0,  1, 2]])

xmid = x[triangles].mean(axis=1)
ymid = y[triangles].mean(axis=1)
x0 = -5
y0 = 52
zfaces = np.exp(-0.01*((xmid - x0)*(xmid - x0) + 
                (ymid - y0)*(ymid - y0)))


plt.figure()
plt.gca().set_aspect('equal')
plt.tripcolor(x, y, triangles, facecolors=zfaces, edgecolors='k')
plt.colorbar()
plt.title('tripcolor of user-specified triangulation')
plt.xlabel('Longitude (degrees)')
plt.ylabel('Latitude (degrees)')

plt.show()

你应该得到以下图片:

【讨论】:

    【解决方案2】:

    您确实可以使用tripcolor 来绘制一组三角形。在下面的代码中,函数quatromatrix 将 4 个二维值数组作为输入进行颜色映射,创建三角形并重新排列颜色以适合各自的位置。因此,它与绘制 4 个 imshow 图非常相似。

    import matplotlib.pyplot as plt
    import numpy as np
    
    def quatromatrix(left, bottom, right, top, ax=None, triplotkw={},tripcolorkw={}):
        if not ax: ax=plt.gca()
        n = left.shape[0]; m=left.shape[1]
    
        a = np.array([[0,0],[0,1],[.5,.5],[1,0],[1,1]])
        tr = np.array([[0,1,2], [0,2,3],[2,3,4],[1,2,4]])
    
        A = np.zeros((n*m*5,2))
        Tr = np.zeros((n*m*4,3))
    
        for i in range(n):
            for j in range(m):
                k = i*m+j
                A[k*5:(k+1)*5,:] = np.c_[a[:,0]+j, a[:,1]+i]
                Tr[k*4:(k+1)*4,:] = tr + k*5
    
        C = np.c_[ left.flatten(), bottom.flatten(), 
                  right.flatten(), top.flatten()   ].flatten()
    
        triplot = ax.triplot(A[:,0], A[:,1], Tr, **triplotkw)
        tripcolor = ax.tripcolor(A[:,0], A[:,1], Tr, facecolors=C, **tripcolorkw)
        return tripcolor
    
    
    right=np.random.randn(8, 8)
    left=np.random.randn(8, 8)
    bottom=np.random.randn(8, 8)
    top=np.random.randn(8, 8)
    
    fig, ax=plt.subplots()
    
    quatromatrix(left, bottom, right, top, ax=ax,
                 triplotkw={"color":"k", "lw":1},
                 tripcolorkw={"cmap": "plasma"}) 
    
    ax.margins(0)
    ax.set_aspect("equal")
    

    【讨论】:

      【解决方案3】:

      我使用 ImportanceOfBeingErnest 的代码为强化学习项目绘制 Q 表 - 我想理解它,所以我通过并使其更清晰一些。只需将数据(上、下、左、右)替换为您自己的数据即可。

      def showQVals(self):
          fig, ax = plt.subplots()
      
          rows = self.level.NUM_ROWS
          cols = self.level.NUM_COLUMNS
      
          up = self.q[:,Action.UP].reshape(rows, cols)
          down = self.q[:,Action.DOWN].reshape(rows, cols)
          right = self.q[:,Action.RIGHT].reshape(rows, cols)
          left = self.q[:,Action.LEFT].reshape(rows, cols)
      
          vertDims = np.array([[0,0],[0,1],[.5,.5],[1,0],[1,1]])
          UP = [1,2,4]
          DOWN = [0,2,3]
          RIGHT = [2,3,4]
          LEFT = [0,1,2]
          triDims = np.array([DOWN, UP, RIGHT, LEFT])
      
          verts = np.zeros((rows*cols*5,2))
          tris = np.zeros((rows*cols*4,3))
      
          for row in range(rows): #i
              for col in range(cols): #j
                  cell = row*cols+col
      
                  #assign slices to the newly constructed verts and tris
                  verts[cell*5:(cell+1)*5,:] = np.c_[vertDims[:,0]+col, vertDims[:,1]+row]
                  tris[cell*4:(cell+1)*4,:] = triDims + cell*5
      
          C = np.c_[ up.flatten(), down.flatten(), 
                  right.flatten(), left.flatten()   ].flatten()
      
          ax.invert_yaxis()
          ax.set_title('Q Values')
      
          triplot = ax.triplot(verts[:,0], verts[:,1], tris)
          tripcolor = ax.tripcolor(verts[:,0], verts[:,1], tris, facecolors=C)
      
          fig.colorbar(tripcolor)
          plt.show()
      

      q table figure based off of grid map

      【讨论】:

        猜你喜欢
        • 2021-12-05
        • 1970-01-01
        • 2021-07-28
        • 2011-09-04
        • 2012-09-21
        • 2016-07-05
        • 1970-01-01
        • 2020-03-11
        • 1970-01-01
        相关资源
        最近更新 更多