python 自定义contourf图的colorbar

bounds1 = [0,2,4,6,10,20,30]

c1 = ax1.contourf(lon_da, lat_da, rms_clim_pre*100,levels=bounds1,zorder=0,extend='both',transform=ccrs.PlateCarree(),cmap=plt.cm.Reds)

 

bounds2 = [-30,-10,-6,-4,-2,0,2,4,6,10,20,30]

#使得colorbar从0开始不同
from matplotlib.colors import BoundaryNorm
cmap = plt.get_cmap('RdBu_r')
norm = BoundaryNorm(bounds2,ncolors=cmap.N,clip=True)

c2 = ax2.contourf(lon_da, lat_da, rms_clim_tmp*100, levels=bounds2,zorder=0,extend='both',transform=ccrs.PlateCarree(),cmap=cmap)

 

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将Matplotlib中的contourf形的colorbar放在下方,可以使用subplot()函数创建一个包含两个子的figure对象,一个子用于绘制contourf形,另一个子用于绘制colorbar。 下面是一个示例代码: ```python import matplotlib.pyplot as plt import numpy as np # 生成数据 x = np.linspace(-3, 3, 301) y = np.linspace(-3, 3, 301) X, Y = np.meshgrid(x, y) Z = np.sin(np.sqrt(X**2 + Y**2)) # 创建figure对象和两个子 fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(8, 4)) # 绘制contourf形 cf = ax1.contourf(X, Y, Z, cmap='coolwarm') ax1.set_xlabel('X') ax1.set_ylabel('Y') ax1.set_title('Contourf Plot') # 绘制colorbar cbar = plt.colorbar(cf, ax=ax1, orientation='horizontal', shrink=0.8, pad=0.15) cbar.ax.set_xlabel('Z') cbar.ax.tick_params(labelsize=8) # 在另一个子中绘制一些其他的形 ax2.plot(x, np.sin(x), label='sin(x)') ax2.plot(x, np.cos(x), label='cos(x)') ax2.set_xlabel('X') ax2.set_ylabel('Y') ax2.set_title('Other Plot') ax2.legend() plt.show() ``` 在这个示例代码中,我们首先使用np.meshgrid()函数生成了一个二维的网格数据,然后使用contourf()函数绘制了一个contourf形,并将其放在了第一个子中。接着,我们使用colorbar()函数在第一个子的右边绘制了一个水平方向的colorbar,并设置了一些参数,如缩放比例和位置等。最后,在第二个子中我们绘制了一些其他的形,如sin(x)和cos(x)。 使用这种方式可以方便地将contourf形和colorbar放在同一个figure中,并且可以灵活地控制它们的位置和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值