【问题标题】:Single plot with three colorbars at the bottom底部有三个颜色条的单图
【发布时间】:2018-10-02 22:06:09
【问题描述】:

我对 Python 比较陌生。我希望从三个颜色条创建一个填充轮廓图,如下所示:http://www.cpc.ncep.noaa.gov/products/predictions/30day/off15_temp.gif

我已经能够创建三个颜色条,每个颜色条的长度都与绘图的一侧相同,并将它们分别放置在左侧、底部和右侧。我的问题有两个:

  1. 如何将颜色条的大小调整为比绘图的一侧短(我已经能够缩小颜色条的宽度,而不是长度。另外,我只能缩小颜色条与情节的一侧一样长)

  2. 如何定位多个颜色条,使它们并排显示在图的底部(我还没有看到在图的一侧有多个颜色条的单一解决方案)

以下是我的部分代码:

import matplotlib.pyplot as plt
import numpy as np

#import data
#lon and lat are arrays representing longitude and latitude respectively
#prob_above, prob_normal and prob_below are arrays representing the probability of above average, normal and below average temperature or precipitation occurring

clevs = np.arange(40,110,10) #percent
cs_above = plt.contourf(lon, lat, prob_above, clevs)
cs_normal = plt.contourf(lon, lat, prob_normal, clevs)
cs_below = plt.contourf(lon, lat, prob_below, clevs)

cbar_above = plt.colorbar(cs_above, location = 'left')
cbar_normal = plt.colorbar(cs_normal, location = 'bottom')
cbar_below = plt.colorbar(cs_below, location = 'right')

【问题讨论】:

    标签: python matplotlib colorbar


    【解决方案1】:

    颜色条是在坐标区中创建的。 要完全控制颜色条所在的位置,您可以在相应位置创建一个轴,并使用颜色条的 cax 参数来指定用于显示颜色条的轴。
    要创建有用的轴,GridSpec 可能会有所帮助,其中主图跨越多个网格单元,并且单元高度的比率非常不对称。

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.gridspec import GridSpec
    
    x,y1,y2,y3 = np.random.rand(4,15)
    
    gs = GridSpec(2,3, height_ratios=[15,1])
    
    fig = plt.figure()
    # Axes for plot
    ax = fig.add_subplot(gs[0,:])
    # three colorbar axes
    cax1 = fig.add_subplot(gs[1,0])
    cax2 = fig.add_subplot(gs[1,1])
    cax3 = fig.add_subplot(gs[1,2])
    
    # plot
    sc1 = ax.scatter(x, y1, c=y1, cmap="viridis")
    sc2 = ax.scatter(x, y2, c=y2, cmap="RdYlGn")
    sc3 = ax.scatter(x, y3, c=y3, cmap="copper")
    
    # colorbars
    fig.colorbar(sc1, cax=cax1, orientation="horizontal")
    fig.colorbar(sc2, cax=cax2, orientation="horizontal")
    fig.colorbar(sc3, cax=cax3, orientation="horizontal")
    
    plt.show()
    

    【讨论】:

    • 感谢您的回复!您的建议有助于回答我的问题。另外,由于我在处理 Basemap,所以我将轴作为参数添加到 Basemap:m = Basemap(projection='merc', lat_0=0, lon_0=0, ax=ax)
    猜你喜欢
    • 2016-06-26
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2019-05-07
    • 1970-01-01
    • 2013-06-04
    相关资源
    最近更新 更多