Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python- How to make colorbar orientation horizontal?

So I have a plot with a basemap, a colormesh on top, and a colorbar set to cbar. I want the colorbar orientation to be horizontal instead of vertical, but when I set orientation='horizontal' in the cbar=m.colorbar line after extend='max', I get the following error: "colorbar() got multiple values for keyword argument 'orientation'"

Someone on another question explained why this happens, but I honestly couldn't understand the answer or see an explanation of how to fix it. Can someone help? I tried using plt.colorbar instead, but for some reason that doesn't accept my tick settings.

Here's what my plot looked like before...

#Set cmap properties
bounds = np.array([0.1,0.2,0.5,1,2,3,4,6,9,13,20,30])
norm = colors.LogNorm(vmin=0.1,vmax=30) #creates logarithmic scale

#Create basemap
fig = plt.figure(figsize=(15.,10.))
m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,llcrnrlon=0,urcrnrlon=360.,lon_0=180.,resolution='c')
m.drawcoastlines(linewidth=1)
m.drawcountries(linewidth=1)
m.drawparallels(np.arange(-90,90,30.),linewidth=0.3)
m.drawmeridians(np.arange(-180.,180.,90.),linewidth=0.3)   
meshlon,meshlat = np.meshgrid(lon,lat)
x,y = m(meshlon,meshlat)

#Plot variables
trend = m.pcolormesh(x,y,lintrends_36,cmap='jet', norm=norm, shading='gouraud')

#Set plot properties
plt.tight_layout()
#Colorbar
cbar=m.colorbar(trend, size='3%',ticks=bounds,extend="max") #THIS LINE
cbar.set_label(label='Linear Trend (mm/day/decade)',size=30)
cbar.set_ticklabels(bounds)
#Titles & labels
plt.suptitle('Linear Trends of Precipitation (CanESM2)',fontsize=40,y=0.962)
plt.title('a) 1979-2014',fontsize=40)
plt.ylabel('Latitude',fontsize=30)
plt.show()

enter image description here

When orientation is attempted (all other code being the same)... enter image description here

And the map looks like this.

enter image description here

like image 527
ChristineB Avatar asked Sep 13 '25 07:09

ChristineB


2 Answers

As others have already stated in the comments

plt.colorbar(orientation='horizontal')

is an alternative (simpler) solution!

like image 134
Zahra Avatar answered Sep 14 '25 19:09

Zahra


You need to use location="bottom"

cbar=m.colorbar(trend, size='3%',ticks=bounds,extend="max",location="bottom")

I got that from this example in the basemap documentation.

enter image description here

like image 35
Ianhi Avatar answered Sep 14 '25 21:09

Ianhi