如何使我的Cartopy彩条具有由我设定的特定范围?

问题描述:

我正在比较各种任务,我希望颜色栏有我设置的最大和最小值。我不知道如何做到这一点,对此有何帮助?任务本身将保持在一个范围内,但我想设置它,因此它很容易进行比较。一些任务是不同的,有些没有,所以我不想让电脑自动设置最大值和最小值。如何使我的Cartopy彩条具有由我设定的特定范围?

enter image description here

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() 
+2

感谢您提供您的代码。提供一些数据来测试它(甚至假设它约为10分或某物)是非常有用的。 FWIW这个问题的答案将与标准的matplotlib标准化有关(即不是专门与cartopy相关的)。 – pelson

我想你必须创建你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允许设置的彩色地图限制。

如果您通过vmin和vmax分散,您可以设置散点图的颜色范围,并且颜色条将相应地设置下注。

例如

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