【问题标题】:How to customize the colorbar in python?如何在python中自定义颜色栏?
【发布时间】:2017-08-29 01:52:05
【问题描述】:

使用此代码,我不知道如何自定义颜色栏。这个webiste 上的颜色图不能让我满意。

shade = m.contourf(Lon,Lat,TBB,np.arange(-90, -20, 10),extend='both',cmap=plt.cm.get_cmap('jet'))       
m.colorbar(shade)

我想用明显的颜色条绘制这样的图片。所以我该怎么做?

【问题讨论】:

    标签: python matplotlib colorbar colormap


    【解决方案1】:

    您可以使用matplotlib.colors.LinearSegmentedColormap()matplotlib.colors.ListedColormap() 定义自己的颜色图并将其用于您的绘图。

    例子:

    import numpy as np; np.random.seed(0)
    import matplotlib.pyplot as plt
    import matplotlib.colors
    
    x = np.arange(0,25)
    a = np.random.randint(0,130, size=(25,25))-115
    a = np.sort(a).reshape(25,25)
    
    colors = ["#eaa941", "#efef39", "#53a447", "#3b387f", "#48a2ba"]
    cmap= matplotlib.colors.ListedColormap(colors)
    cmap.set_under("crimson")
    cmap.set_over("w")
    norm= matplotlib.colors.Normalize(vmin=-100,vmax=-0)
    
    fig, ax = plt.subplots()
    im = ax.contourf(x,x,a, levels=[-100,-80,-60,-40,-20,0],
                     extend='both',cmap=cmap, norm=norm)
    
    fig.colorbar(im, extend="both")
    
    plt.show()
    

    【讨论】:

    • colors = ["#eaa941", "#efef39", "#53a447", "#3b387f", "#48a2ba"] cmap= matplotlib.colors.ListedColormap(colors) cmap.set_under("crimson") cmap.set_over("w") norm= matplotlib.colors.Normalize(vmin=-80,vmax=-30) shade=m.contourf(Lon,Lat,TBB,extend='both',cmap=cmap,norm=norm) m.colorbar(shade) 然后,我得到了这个picture,为什么?
    • 您还需要在轮廓图中定义级别。我更新了答案以实际使用轮廓图。
    • 你的答案是对的。但是,我想在地图上添加颜色。所以,我试试这个代码:im = m.contourf(Lon,Lat,TBB,levels=[-80,-70,-60,-50,-40,-30],extend='both',cmap=cmap,norm=norm) m.colorbar(im) 和其他代码和你的一样。它工作正常,图片是here
    • 有没有办法合并颜色而不是离散颜色?
    • @RitambharaChauhan 等高线图的概念是具有离散水平。显然,如果你采取足够的水平,情节可能看起来比离散的更连续。
    【解决方案2】:

    看起来很像spectral 颜色图,它在matplotlib 页面上给出..

    【讨论】:

    • 我可以为不同的值范围定制不同的颜色吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 2018-03-18
    • 1970-01-01
    相关资源
    最近更新 更多