Python-如何使颜色条方向水平? [英] Python- How to make colorbar orientation horizontal?

查看:98
本文介绍了Python-如何使颜色条方向水平?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

有人在另一个问题上解释了为什么会发生这种情况,但老实说,我无法理解答案或看到如何解决它的解释.有人可以帮忙吗?我尝试使用 plt.colorbar 代替,但由于某种原因不接受我的刻度设置.

这是我之前的情节……

#设置cmap属性边界 = 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) #创建对数刻度#创建底图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(线宽=1)m.drawcountries(线宽=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)#绘制变量趋势 = m.pcolormesh(x,y,lintrends_36,cmap='jet', norm=norm, shading='gouraud')#设置绘图属性plt.tight_layout()#颜色条cbar=m.colorbar(trend, size='3%',ticks=bounds,extend="max") #这条线cbar.set_label(label='线性趋势 (mm/day/decade)',size=30)cbar.set_ticklabels(bounds)#Titles &标签plt.suptitle('降水的线性趋势 (CanESM2)',fontsize=40,y=0.962)plt.title('a) 1979-2014',fontsize=40)plt.ylabel('纬度',fontsize=30)plt.show()

尝试定位时(所有其他代码都相同)...

地图看起来像这样.

解决方案

需要使用location="bottom"

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

我从底图文档中的

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

When orientation is attempted (all other code being the same)...

And the map looks like this.

解决方案

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.

这篇关于Python-如何使颜色条方向水平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆