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

标签 python matplotlib orientation colorbar

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

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

这是我之前的情节......

#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

尝试定向时(所有其他代码相同)... enter image description here

map 看起来像这样。

enter image description here

最佳答案

你需要使用location="bottom"

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

我从 this 得到的 basemap 文档中的示例。

enter image description here

关于Python-如何使颜色条方向水平?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38487440/

相关文章:

适用于 Windows、matplotlib 和 opencv 的 Python

xcode - 如何在 iOS 模拟器中设置默认方向

javascript - 窗口在方向更改时调整内容大小之前的触发功能?

python - 使用滚动窗口 Pandas 计算百分位数

python - Matplotlib 未绘制整条线

python - 减少matplotlib图中的左右边距

android - 更改方向时如何在 ViewFlipper 中保持当前 View

python - 将图例或背景图像添加到 Igraph 0.6 (for python) 图中

python - 无法导入 Decimal 模块

python - Django 自定义用户字段与 AbstractBaseUser 冲突