【问题标题】:Is it possible to assign a colourmap for edgecolors in matplotlib?是否可以在 matplotlib 中为边缘颜色分配颜色图?
【发布时间】:2022-04-07 17:07:33
【问题描述】:

假设需要根据某个变量更改 matplotlib 标记的边缘颜色,是否可以为标记的边缘颜色分配某种离散颜色图? 这类似于通过 cmap 更改标记的面颜色。

当使用绘图范围之外的箭头显示限制时,我似乎无法根据另一个变量来改变箭头颜色。 例如:在下面的代码中,箭头的颜色不会随着 z 的变化而变化。

plt.scatter(x,y, c=z, marker=u'$\u2191$', s=40,cmap=discrete_cmap(4, 'cubehelix') )

【问题讨论】:

    标签: python matplotlib colors


    【解决方案1】:

    您可以使用 edgecolors 参数来分散。

    您需要列出要提供给scatter 的颜色列表。我们可以使用您选择的colormapNormalize 实例来执行此操作,将z 函数重新缩放到0-1 范围。

    我假设您的 discrete_cmap 函数类似于链接的 here

    import matplotlib.pyplot as plt
    import matplotlib.colors as colors
    import numpy as np
    
    # def discrete_cmap() is omitted here...
    
    # some sample data
    x = np.linspace(0,10,11)
    y = np.linspace(0,10,11)
    z = x+y
    
    # setup a Normalization instance
    norm = colors.Normalize(z.min(),z.max())
    
    # define the colormap
    cmap = discrete_cmap(4, 'cubehelix')
    
    # Use the norm and cmap to define the edge colours
    edgecols = cmap(norm(z))
    
    # Use that with the `edgecolors` argument. Set c='None' to turn off the facecolor
    plt.scatter(x,y, edgecolors=edgecols, c = 'None', marker='o', s=40 )
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 2019-03-08
      相关资源
      最近更新 更多