【问题标题】:How do I make my colour bar for Cartopy have a specific range set by me?如何使 Cartopy 的颜色条具有我设置的特定范围?
【发布时间】:2018-02-12 02:31:14
【问题描述】:

我正在比较各种任务,我希望彩条有我设置的最大值和最小值。我不知道该怎么做,有什么帮助吗?任务本身会保持在一个范围内,但我想设置它,所以很容易比较。有些任务不同,有些则没有,所以我不希望计算机自动设置最大值和最小值。

crs_latlon = ccrs.PlateCarree()
def make_plot(projection_name, projection_crs):

    ax = plt.axes(projection=projection_crs)
    #ax.set_extent((-65.0, -58, 40, 47.7), crs=crs_latlon)
    ax.set_extent((-65.0, -62, 43, 45.5), crs=crs_latlon)

    #Add coastlines and meridians/parallels (Cartopy-specific).
    plt.gca().coastlines('10m')

    ax.gridlines(crs=crs_latlon, linestyle='-')
    # Add a title, legend, and display.
    ax.set_title(''.join(("Mission #13: Attenuation Coeffiecient - ",
                      projection_name)))

    cb = plt.scatter(avglonlist, avglatlist, c=klist, cmap=coolwarm)
    plt.colorbar(cb, cmap=coolwarm, orientation='vertical',ticklocation='auto')
    #plt.colorbar.ColorbarBase(ax=ax, cmap = coolwarm, orientation='vertical', ticklocation='auto',
     #                        norm=plt.colors.Normalize(vmin=0, vmax=1))
    iplt.show()

def main():
# Demonstrate with two different display projections.
    make_plot('Equidistant Cylindrical', ccrs.PlateCarree())
    graph_my_latandk(avglatlist,klist)

if __name__ == '__main__':
    main()

【问题讨论】:

  • 感谢您提供代码。提供一些数据来测试它会非常有用(即使你用大约 10 分或其他东西伪造它)。 FWIW 这个问题的答案将与标准 matplotlib 规范化有关(即与 cartopy 无关)。

标签: python matplotlib mapping matplotlib-basemap cartopy


【解决方案1】:

我猜你必须像这里一样创建你的own color bar

import matplotlib.pylab as plt
from matplotlib import colorbar, colors

fig = plt.figure(figsize=(8, 3))
ax = fig.add_axes([.05, .05, .05, .7]) # position of colorbar
cbar = colorbar.ColorbarBase(ax, cmap=plt.get_cmap('coolwarm'),
  norm=colors.Normalize(vmin=-.5, vmax=1.5)) # set min, max of colorbar
cbar.set_clim(-.5, .5) # set limits of color map
plt.show()

vmin, vmax 允许设置颜色条的限制,但clim 允许设置颜色映射的限制。

【讨论】:

    【解决方案2】:

    如果你将 vmin 和 vmax 传递给 scatter,你可以设置散点图的颜色范围,colorbar 会相应设置。

    例如

    cb = plt.scatter(avglonlist, avglatlist, c=klist, cmap=coolwarm, vmin=-4, vmax=2)
    plt.colorbar(cb, cmap=coolwarm, orientation='vertical',ticklocation='auto')
    

    【讨论】:

      猜你喜欢
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 2017-08-01
      相关资源
      最近更新 更多