代码之家  ›  专栏  ›  技术社区  ›  JKGalbraith

使用Matplotlib基于阵列将颜色渲染为箭头

  •  0
  • JKGalbraith  · 技术社区  · 3 年前

    V ? 根据预期输出分配值。

    import numpy as np
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    from matplotlib.colors import Normalize
    import matplotlib.colors as colors
    from matplotlib import cm
    
    
    V=np.array([[10],[20],[30],[40],[50],[60]])
    
    cmap = plt.cm.jet
    cNorm  = colors.Normalize(vmin=np.min(V), vmax=np.max(V))
    scalarMap = cm.ScalarMappable(norm=cNorm,cmap=cmap)
    
    
    fig = plt.figure()
    ax  = fig.add_axes([0.1, 0.1, 0.7, 0.855]) # [left, bottom, width, height]
    axc = fig.add_axes([0.85, 0.10, 0.05, 0.85])
    
            
    for idx in range(0,len(V)):
            colorVal = scalarMap.to_rgba(V[idx])
            ax.arrow(200, 400, -50, -50,width=5, head_length=20, fc='k',ec='k')
            ax.arrow(200, 400, 50, -50, width=5, head_length=20, fc='k', ec='k')
            ax.arrow(200, 400, 0, -50, width=5, head_length=20, fc='k', ec='k')
            ax.arrow(200, 300, 50, 0, width=5, head_length=20, fc='k', ec='k')
            ax.arrow(200, 300, 50, -50, width=5, head_length=20, fc='k', ec='k')
            ax.arrow(400, 300, -50, -50, width=5, head_length=20, fc='k', ec='k')  
            cb1 = mpl.colorbar.ColorbarBase(axc, cmap=cmap,
                                                norm=cNorm,orientation='vertical')
            
    ax.set_xlim(left = 0, right = 500)
    ax.set_ylim(bottom = 0, top = 500)
    plt.show()
    

    enter image description here

    预期输出为

    enter image description here

    1 回复  |  直到 3 年前
        1
  •  1
  •   r-beginners    3 年前

    由于RGBA值是在循环过程中获得的,因此它准备了一个空列表并获得每个箭头的颜色。从获取的列表中,指定结果列表中的颜色。

    import numpy as np
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    from matplotlib.colors import Normalize
    import matplotlib.colors as colors
    from matplotlib import cm
    
    V=np.array([[10],[20],[30],[40],[50],[60]])
    
    cmap = plt.cm.jet
    cNorm  = colors.Normalize(vmin=np.min(V), vmax=np.max(V))
    scalarMap = cm.ScalarMappable(norm=cNorm,cmap=cmap)
    
    
    fig = plt.figure()
    ax  = fig.add_axes([0.1, 0.1, 0.7, 0.855]) # [left, bottom, width, height]
    axc = fig.add_axes([0.85, 0.10, 0.05, 0.85])
    
    color_list = []        
    for idx in range(0,len(V)):
            colorVal = scalarMap.to_rgba(V[idx])
            r,g,b,a = colorVal[0].tolist()
            color_list.append((r,g,b,a))
            
    ax.arrow(200, 400, -50, -50,width=5, head_length=20, fc=color_list[0],ec=color_list[0])
    ax.arrow(200, 400, 50, -50, width=5, head_length=20, fc=color_list[1], ec=color_list[1])
    ax.arrow(200, 400, 0, -50, width=5, head_length=20, fc=color_list[2], ec=color_list[2])
    ax.arrow(200, 300, 50, 0, width=5, head_length=20, fc=color_list[3], ec=color_list[3])
    ax.arrow(200, 300, 50, -50, width=5, head_length=20, fc=color_list[4], ec=color_list[4])
    ax.arrow(400, 300, -50, -50, width=5, head_length=20, fc=color_list[5], ec=color_list[5])  
    cb1 = mpl.colorbar.ColorbarBase(axc, cmap=cmap,
                                        norm=cNorm,orientation='vertical')
            
    ax.set_xlim(left = 0, right = 500)
    ax.set_ylim(bottom = 0, top = 500)
    plt.show()
    

    enter image description here