gpt4 book ai didi

python - 如何缩小颜色条占用的区域?

转载 作者:太空宇宙 更新时间:2023-11-03 18:45:18 25 4
gpt4 key购买 nike

我想在一个图中获得两个颜色条,并带有 map 。不幸的是,颜色条和情节本身一样大。即使在颜色条代码中使用收缩,它也只会缩小颜色条,而不会缩小它们占用的大小。

有没有一种简单的方法可以为我的绘图使用更多的空间,并为颜色条使用更少的空间?有没有一种简单的方法可以让颜色条也并排出现在底部?

Plot with colourbars代码如下

plt.clf()
my_cmap = cm.get_cmap('YlOrRd')


cs = map.contourf(x,y,bj,levels = Y,cmap=my_cmap,locator=mpl.ticker.LogLocator())


norm = mpl.colors.BoundaryNorm(bounds, my_cmap.N)
cb1 = plt.colorbar(cmap=my_cmap,
norm=norm,
boundaries=bounds,
extend='both',
orientation="horizontal",
ticks=bounds,
shrink = 0.35)
cb1.set_label('Increase in Black Carbon')
bj = -bj
ymap = cm.get_cmap('PuBu')


cs = map.contourf(x,y,bj,levels = Y,cmap=ymap,locator=mpl.ticker.LogLocator())

# set colourbar with location and size, with labels.
norm = mpl.colors.BoundaryNorm(bounds,ymap.N)
cb2 = plt.colorbar(cmap=my_cmap,
norm=norm,
boundaries=bounds,
extend='both',
orientation="horizontal",
ticks=bounds,
shrink=0.35)


cb2.set_label('Decrease in Black Carbon')

font = {'family' : 'serif',
'color' : 'black',
'weight' : 'bold',
'size' : 21,
}

#add plot details
plt.title(r'Black Carbon surface concentrations changes in %s 2006 compared with %s 2006 ($\mu$gm$\^3$)'%(g,d) ,fontdict=font)
map.drawcoastlines(linewidth=0.75)
map.drawcountries(linewidth=0.25)
#show plot
plt.show()

最佳答案

您的问题的最小工作示例*和一些more options for plt.colorbar实现您正在寻找的东西:

import pylab as plt

plt.imshow([[1,2,3],[4,5,6]])

cbar_options = {'extend':'both',
'orientation':"horizontal",
'shrink':0.75,
'fraction':.10,
'pad':.07}

cb1 = plt.colorbar(**cbar_options)
cb1.set_label('Increase in Black Carbon')

cb2 = plt.colorbar(**cbar_options)
cb2.set_label('Decrease in Black Carbon')

plt.show()

enter image description here

  • 您应该始终尝试发布开箱即用的最少代码片段。您的示例需要大量的摆弄,并且缺少导入和变量!

关于python - 如何缩小颜色条占用的区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19665148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com