python - 为什么 matplotlib 颜色条不显示任何刻度线和标签

标签 python matplotlib colorbar

我一直在尝试根据一些数据绘制带有颜色的形状文件多边形,并添加一个颜色条。下面显示的是我为此目的编写的代码。它给出了正确的绘图,但颜色条没有标签或刻度线。

fig, ax = plt.subplots(1,1,figsize=(12,10), subplot_kw={'projection': ccrs.PlateCarree()})
ptchs1   = []
for nshp in indx[0,:]:
    ptchs   = []
    pts     = np.array(shapes[nshp].points)
    pts     =pts[np.unique(np.where(~np.isnan(pts[:,:]))[0]),:]
    prt     = shapes[nshp].parts
    par     = list(prt) + [pts.shape[0]]
    for pij in xrange(len(prt)):
        ptchs.append(Polygon(pts[par[pij]:par[pij+1]]))
        ptchs1.append(Polygon(pts[par[pij]:par[pij+1]]))
    ind=np.where(indx==nshp)[1] ; 
    ax.add_collection(PatchCollection(ptchs,facecolor=my_cmap[np.where(indx==nshp)[1],:],edgecolor='k', linewidths=.5),ccrs.PlateCarree())
    plt.text(xtext[ind], ytext[ind],dnme[ind][0:7],fontsize=8,color='k',fontweight='bold', ha='center',va='center',transform=ccrs.Geodetic())
ax.set_xlim(np.round(mnbbx[0]).astype(int)-0.5,np.round(mxbbx[2]).astype(int)+0.5,2)
ax.set_ylim(np.round(mnbbx[1]).astype(int)-0.5,np.round(mxbbx[3]).astype(int)+0.5,2) 
m = cm.ScalarMappable(cmap=cmap)
m.set_array([])
m.set_clim(-0.5, 14+0.5) 
divider = make_axes_locatable(ax)
cax = divider.append_axes("right",aspect=20,size="5%",pad=0.05,map_projection=ccrs.PlateCarree())    
cb=fig.colorbar(m,cax=cax,norm=norm)
cb.set_ticks(np.arange(0,14))
cb.set_label('RainFall(mm/day)', rotation=90)   
cb.set_ticklabels([1,5,10,20,30,40,70,100,130,160,190,220,250])
xticks = np.arange(np.round(mnbbx[0]).astype(int)-0.5,np.round(mxbbx[2]).astype(int)+0.5,2)
yticks = np.arange(np.round(mnbbx[1]).astype(int)-0.5,np.round(mxbbx[3]).astype(int)+0.5,2)

gl = ax.gridlines(crs=ccrs.PlateCarree(),draw_labels=True,linewidth=0.7, color='black', alpha=1)
gl.xlabels_bottom = False ;     gl.ylabels_right = False
degree_locator = mticker.MaxNLocator(nbins=4) #it will give gridlines of 4*4 size
gl.xlocator = degree_locator
gl.ylocator = degree_locator
_DEGREE_SYMBOL = u'\u00B0'
gl.xformatter = LONGITUDE_FORMATTER #it will change lon to degree format
gl.yformatter = LATITUDE_FORMATTER
gl.ylabel_style = {'size': 12, 'color': 'black'} # here we can adjust color and size of ticks
gl.xlabel_style = {'color': 'black', 'size': 12 }
plt.show()

任何人都可以帮助我,为什么没有生产颜色条刻度线和标签? enter image description here

我已经用这段代码更新了我的彩条绘图,它绘图正确,但主图和彩条大小不同(大或小)

    col_bnd=[0,1,   5,  10,  20,  30,  40,  70, 100, 130, 160, 190, 220, 250,300]
    norm = colors.BoundaryNorm(col_bnd, cmap.N)
    col_bnd=[1,   5,  10,  20,  30,  40,  70, 100, 130, 160, 190, 220, 250]
    ax1, ax2 = mpl.colorbar.make_axes(ax, shrink=0.68,aspect=20,pad=0.05)
    cbar = mpl.colorbar.ColorbarBase(ax1,    cmap=cmap,norm=norm,ticks=col_bnd,boundaries=None,format='%1i') #mpl.colors.Normalize(vmin=-0.5, vmax=1.5))
    cbar.set_clim(0, 300)
    cbar.set_label('RainFall(mm/day)', rotation=90)   

谁能告诉我,如何使主图和颜色条大小相同?

最佳答案

[编辑]

正如下面的评论,颜色条似乎需要一些不同的方式来显示标签。你可以试试这个:

cb.set_yticklabels([1,5,10,20,30,40,70,100,130,160,190,220,250])

如果您正在进行其他绘图,您应该为您在代码中创建的每个艺术家调用“图例”函数。例如,您有 ax 艺术家,并在其上设置了所有必要的信息,但图例除外。 所以你应该在 plt.show() 之前调用它,这样它才能正确生成图例:

ax.legend()
fig.legend()

等等。

该文档对legendartists 函数有很好的指导:

关于python - 为什么 matplotlib 颜色条不显示任何刻度线和标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30868126/

相关文章:

Python - 有一个 pcolor 图;现在想要一个表面

python - 如何将最佳拟合线放置到绘制的点上?

python - python 中我的颜色条范围是多少?我如何获得句柄?

python - 根据元组中的索引删除重复的元组值

python - 使用管道作为估计器的 VotingClassifier

python - 在 Ubuntu 18.04 上使用 Python3.6 安装 basemap

python - 将颜色条作为图例添加到 matplotlib 散点图(多个子图,多个散点图)

python - 从 python 评估 "Shell command line with shell variables"或将 python 字符串评估为 shell 命令行

python - Pandas dropna() 不删除整行

python - Matplotlib Colorbar 更改刻度标签和定位器