How to change the shape of a custom color bar using matplotlib?

282 Views Asked by At

My data has integer values mapped to categorical variables and I created a custom color bar to reflect these variables. When I've done this previously, I needed to add an extra color in my list and add the 0 as an integer to balance out the small triangle at the bottom since it isn't visible enough to be a part of a good plot. However, I have many more categorical variables for the plot I am trying to make and one of them uses 0 as an integer, and the triangle color is my desired first color. Is there a way to change the shape of my color bar instead of adding an extra integer to my levels index like I did previously? A rectangle/bar would be perfect, but for whatever reason it isn't doing that.

This is my code:

ax = plt.axes(projection=ccrs.Geostationary(central_longitude=-75))
ax.set_xlim((-3627271.341, 1404173.822))                                       
ax.set_ylim((1564871.9518, 4600199.7649))   
ax.coastlines(resolution='10m', linewidth=0.5, color='black')
ax.add_feature(cartopy.feature.BORDERS, linewidth=0.5, zorder=0, edgecolor='black')
ax.add_feature(cartopy.feature.LAKES, linewidth=0.5, zorder=0, edgecolor='black')
ax.add_feature(cartopy.feature.STATES, linewidth=0.3, edgecolor='black')
​
cMap = ListedColormap(['#0aa60f', 'white', 'black', '#f2d0ed', '#c991c1', '#e36212', '#f2aa24', 'yellow', '#dfe09d', '#c9c7c9', '#f207f2', '#1f3ef0', '#1991e0', '#02f6fa', 
                       '#6ae68d', '#5c081f'])
levels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
​
my_data = scn[product]
figr = my_data.plot.imshow(transform=crs, cmap = cMap, levels = levels, add_colorbar=False)
​
cbar = plt.colorbar(figr)
cbar.ax.get_yaxis().set_ticks([])
for j, lab in enumerate(['clear','probably clear','fog','water', 'supercld water', 'mixed', 'opaque ice', 'cirrus', 'overlapping', 'overshooting', 'unknown', 'dust', 'smoke', 
                         'fire']):
    cbar.ax.text(4.2, (2.2 * j + 1) / 2.325, lab, ha='center', va='center')
    
plt.title('Cloud Type on May 4, 2022 over CONUS')

This is the image I am currently getting: https://i.stack.imgur.com/jYAnp.png

Any help would be appreciated, as I am just learning how to do this. Thanks!

1

There are 1 best solutions below

0
On

I figured out what it was. I took levels = levels out of the 'figr =' line and put vmin=0, vmax=13 instead.