Why are labels and labels not showing in matplotlib color bar

I am trying to plot polygons with shape files with colors according to some data and add a color bar. Below is the code I wrote for this purpose. This gives the graph correctly, but there are no labels or labels for the color bar.

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

      

Can anyone help me on why no color-coded markings and labels are produced? enter image description here

I have updated my color plot with this code snippet, it displays correctly but the size of the main and color bars is different (large or small)

    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)   

      

can anyone tell me how to make both the main plot and a colored stripe of the same size?

+3


source to share


1 answer


[change]

As stated below, it seems that the colored bars need a different way to display the labels. You can try this:

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

      

You should call the function "legend" for every performer you create in your code if you do other conspiracies. For example, you have an artist ax

and you set all the necessary information for him except for the legend. Therefore, you must call it before plt.show()

so that it can generate the legend correctly:



ax.legend()
fig.legend()

      

Etc.

The documentation has very good guides for functions legend

and artists

:

-1


source







All Articles