How to use viridis in matplotlib 1.4

后端 未结 4 1848
陌清茗
陌清茗 2020-12-31 02:59

I want to use the colormap \"viridis\" (http://bids.github.io/colormap/), and I won\'t be updating to the development version 1.5 quite yet. Thus, I have downloaded c

相关标签:
4条回答
  • 2020-12-31 03:52

    What I did is to just copy the

    _viridis_data = [[0.267004, 0.004874, 0.329415],
                     [0.268510, 0.009605, 0.335427],
                     [0.269944, 0.014625, 0.341379],
                     :
                     [0.983868, 0.904867, 0.136897],
                     [0.993248, 0.906157, 0.143936]]
    

    from https://github.com/BIDS/colormap/blob/master/colormaps.py

    and add:

    from matplotlib.colors import ListedColormap
    
    viridis = ListedColormap(_viridis_data, name='viridis')
    
    plt.register_cmap(name='viridis', cmap=viridis)
    plt.set_cmap(viridis)
    
    0 讨论(0)
  • 2020-12-31 03:56

    To set viridis as your colormap using set_cmap, you must register it first:

    import colormaps as cmaps
    plt.register_cmap(name='viridis', cmap=cmaps.viridis)
    plt.set_cmap(cmaps.viridis)
    
    img=mpimg.imread('stinkbug.png')
    lum_img = np.flipud(img[:,:,0])
    imgplot = plt.pcolormesh(lum_img)
    
    0 讨论(0)
  • 2020-12-31 03:59

    Download the colormaps.py from here,then:

    import os,sys
    scriptpath = "/Your downloading path/colormap-master/"
    sys.path.append(os.path.abspath(scriptpath))
    import colormaps as cmaps   
    

    Done!

    0 讨论(0)
  • 2020-12-31 04:02

    Rather than using set_cmap, which requires a matplotlib.colors.Colormap instance, you can set the cmap directly in the pcolormesh call

    (cmaps.viridis is a matplotlib.colors.ListedColormap)

    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    import numpy as np
    
    import colormaps as cmaps
    
    img=mpimg.imread('stinkbug.png')
    lum_img = np.flipud(img[:,:,0])
    
    imgplot = plt.pcolormesh(lum_img, cmap=cmaps.viridis)
    
    0 讨论(0)
提交回复
热议问题