简体   繁体   English

Python-如何使颜色条方向水平?

[英]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.所以我有一个带有底图的绘图,顶部有一个颜色网格,以及一个设置为 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'"我希望颜色条的方向是水平的而不是垂直的,但是当我在 extend='max' 之后的 cbar=m.colorbar 行中设置orientation='horizo​​ntal' 时,我收到以下错误:“colorbar() got multiple values for关键字参数'方向'"

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.我尝试使用 plt.colorbar 代替,但由于某种原因不接受我的刻度设置。

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

在此处输入图片说明

When orientation is attempted (all other code being the same)...尝试定位时(所有其他代码都相同)... 在此处输入图片说明

And the map looks like this.地图看起来像这样。

在此处输入图片说明

You need to use location="bottom"您需要使用location="bottom"

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

I got that from this example in the basemap documentation.我从底图文档中的这个例子中得到了这一点

在此处输入图片说明

As others have already stated in the comments正如其他人在评论中已经指出的那样

plt.colorbar(orientation='horizontal')

is an alternative (simpler) solution!是一种替代(更简单)的解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM