python AttributeError: ‘Colorbar‘ object has no attribute ‘set_clim‘

运行程序时出现‘Colorbar’ object has no attribute ‘set_clim’错误,原因是matplotlib版本过高。matplotlib 3.3.0版本取消了Colorbar的set_clim方法,可通过降低版本到3.2.0解决,操作是先卸载再安装指定版本。

运行程序显示错误:AttributeError: ‘Colorbar’ object has no attribute ‘set_clim’
解决:
是matplotlib版本过高所致
matplotlib3.3.0版本取消了Colorbar的set_clim方法,因此需要降低版本3.2.0即可解决问题
首先
pip uninstall matplotlib
之后
pip install matplotlib==3.2.0

# 导入所需库 import numpy as np import pandas as pd import xarray as xr import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.feature as cfeature from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas # import matplotlib.backends.backend_agg as agg # 更改字体 plt.rcParams['font.sans-serif'] = ['Times New Roman'] plt.rcParams['axes.unicode_minus'] = False # 处理数据 f = xr.open_dataset('D:\\wangnan\\2024sjxm\\shuju\\spei01new.nc') # 读取数据集 spei81_10 = f.spei.loc[f.time.dt.month.isin([8]), 35:38].loc['1991':'2020'] spei11 = f.spei.loc['2021-08', 35:38].squeeze() # 2021年8月数据 x1 = spei81_10.mean('time') # 8月(1990-2020)平均 x2 = spei11 - spei81_10.mean('time') # 2021年1月距平 x3 = spei11 - spei11.mean('lon') # 2021年1月纬偏值 # 绘画气候态 def clim_plot(): fig = plt.figure(figsize=(6, 6), facecolor='w') ax = fig.add_axes([0.2, 0.2, 0.7, 0.7], projection=ccrs.PlateCarree(180)) ax.set_title(' Summer(1991-2020) Ave Spei', loc='left', fontsize=13) # ax.add_feature(cfeature.COASTLINE.with_scale('110m'), linewidth=0.6, edgecolor='k') ax.add_feature(cfeature.BORDERS, lw=0.25) ax.add_feature(cfeature.STATES, lw=0.25) ax.set_xticks(np.arange(104, 107, 1), crs=ccrs.PlateCarree()) ax.set_yticks(np.arange(35, 38, 1), crs=ccrs.PlateCarree()) ax.xaxis.set_major_formatter(LongitudeFormatter()) ax.yaxis.set_major_formatter(LatitudeFormatter()) ax.tick_params(which='major', width=0.5, length=5) ax.set_aspect(1.5) ax.spines['geo'].set_linewidth(0.5) plt.xticks(rotation=45) # ax.scatter(27, 99, color='blue', marker="o", s=5) # plt.text(27+0.001, 99+0.001, "四川", fontsize=20, color="k", horizontalalignment='right', fontproperties="SimHei") m = ax.contourf(f.lon, f.lat.loc[35:38], x1, cmap='seismic_r', levels=np.arange(-1.5, 1.5, 0.2), transform=ccrs.PlateCarree()) cax = fig.add_axes([0.26, 0.1, 0.57, 0.017]) cb = fig.colorbar(m, cax=cax, orientation='horizontal', ticks=np.arange(-1.4, 1.4, 0.2)) cb.outline.set_linewidth(0.1) cb.ax.tick_params(length=0) clim_plot() plt.show()这段代码中报的错AttributeError: 'FigureCanvasInterAgg' object has no attribute 'tostring_rgb'. Did you mean: 'tostring_argb'?怎么修改?
最新发布
09-10
# 导入所需库 import numpy as np import pandas as pd import xarray as xr import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.feature as cfeature from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas # import matplotlib.backends.backend_agg as agg # 更改字体 plt.rcParams['font.sans-serif'] = ['Times New Roman'] plt.rcParams['axes.unicode_minus'] = False # 处理数据 f = xr.open_dataset('D:\\wangnan\\2024sjxm\\shuju\\spei01new.nc') # 读取数据集 spei81_10 = f.spei.loc[f.time.dt.month.isin([8]), 35:38].loc['1991':'2020'] spei11 = f.spei.loc['2021-08', 35:38].squeeze() # 2021年8月数据 x1 = spei81_10.mean('time') # 8月(1990-2020)平均 x2 = spei11 - spei81_10.mean('time') # 2021年1月距平 x3 = spei11 - spei11.mean('lon') # 2021年1月纬偏值 # 绘画气候态 def clim_plot(): fig = plt.figure(figsize=(6, 6), facecolor='w') ax = fig.add_axes([0.2, 0.2, 0.7, 0.7], projection=ccrs.PlateCarree(180)) ax.set_title(' Summer(1991-2020) Ave Spei', loc='left', fontsize=13) # ax.add_feature(cfeature.COASTLINE.with_scale('110m'), linewidth=0.6, edgecolor='k') ax.add_feature(cfeature.BORDERS, lw=0.25) ax.add_feature(cfeature.STATES, lw=0.25) ax.set_xticks(np.arange(104, 107, 1), crs=ccrs.PlateCarree()) ax.set_yticks(np.arange(35, 38, 1), crs=ccrs.PlateCarree()) ax.xaxis.set_major_formatter(LongitudeFormatter()) ax.yaxis.set_major_formatter(LatitudeFormatter()) ax.tick_params(which='major', width=0.5, length=5) ax.set_aspect(1.5) ax.spines['geo'].set_linewidth(0.5) plt.xticks(rotation=45) # ax.scatter(27, 99, color='blue', marker="o", s=5) # plt.text(27+0.001, 99+0.001, "四川", fontsize=20, color="k", horizontalalignment='right', fontproperties="SimHei") m = ax.contourf(f.lon, f.lat.loc[35:38], x1, cmap='seismic_r', levels=np.arange(-1.5, 1.5, 0.2), transform=ccrs.PlateCarree()) cax = fig.add_axes([0.26, 0.1, 0.57, 0.017]) cb = fig.colorbar(m, cax=cax, orientation='horizontal', ticks=np.arange(-1.4, 1.4, 0.2)) cb.outline.set_linewidth(0.1) cb.ax.tick_params(length=0) clim_plot() plt.show()运行以上代码并将错误的地方标出来改正
09-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值