爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 1141|回复: 3

[求助] 求助python绘制不等间距数据填色图,和等间距colorbar

[复制链接]

新浪微博达人勋

发表于 2023-9-10 15:44:36 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
我想绘制不等间距的填色图,但是不知道怎么设置,求大佬们看看!!
from matplotlib.colors import ListedColormap
import matplotlib.colors as mcolors
rgb=(
        [ 255,255,255],
        [ 203,205,252],
        [ 91,90,254],
        [ 8,97,163],
        [ 15,173,61],
        [ 160,226,13],
        [ 219,244,2],
        [ 254,213,0],
        [ 255,133,5],
        [ 253,94,3]
        )
rgb=np.array(rgb)/255.0
new_cmap=ListedColormap(rgb,name='new_cmap')

bcfile=xr.open_dataset("F:/degar_HTAPv3_2013/edgar_HTAPv3_2013_BC/edgar_HTAPv3_2013_BC.nc")
#print(bcfile)
x=['HTAPv3_1_International_Shipping','HTAPv3_2_1_Domestic_Aviation','HTAPv3_2_2_International_Aviation','HTAPv3_3_Energy',\
   'HTAPv3_4_1_Industry','HTAPv3_4_2_Fugitive','HTAPv3_4_3_Solvents','HTAPv3_5_1_Road_Transport','HTAPv3_5_2_Brake_and_Tyre_wear',\
  'HTAPv3_5_3_Domestic_shipping','HTAPv3_5_4_Other_ground_transport','HTAPv3_6_Residential','HTAPv3_7_Waste',\
   'HTAPv3_8_1_Agricultural_waste_burning','HTAPv3_8_2_Agriculture_livestock','HTAPv3_8_3_Agriculture_crops']
title=['International_Shipping','Domestic_Aviation','International_Aviation','Energy',\
   'Industry','Fugitive','Solvents','Road_Transport','Brake_and_Tyre_wear',\
  'Domestic_shipping','Other_ground_transport','Residential','Waste',\
   'Agricultural_waste_burning','Agriculture_livestock','Agriculture_crops']
lat=bcfile['lat']
lon=bcfile['lon']
fig=plt.figure(figsize=(10,9))
norm = mcolors.Normalize(vmin=0, vmax=9)
for i in range(16):
    data=bcfile[x[i]].loc['2013-07-15',:,:]
    axe=plt.subplot(4,4,i+1,projection=ccrs.PlateCarree())
    clevs_500_t = np.array([0,0.005,0.05,0.1,0.5,1,1.5,2,3,5,9])
    cf = axe.contourf(lon, lat, data, clevs_500_t, cmap=new_cmap,norm=norm,transform=ccrs.PlateCarree(),
                      zorder=0)
   
    axe.add_feature(cfeature.COASTLINE.with_scale('50m'), linewidth=0.5,color='k')
    axe.set_extent([105,135, 22, 45], crs=ccrs.PlateCarree())
    axe.set_xticks(np.arange(110, 135+5, 10), crs=ccrs.PlateCarree())
    axe.set_yticks(np.arange(27, 47, 10), crs=ccrs.PlateCarree())
    axe.xaxis.set_major_formatter(LongitudeFormatter())
    axe.yaxis.set_major_formatter(LatitudeFormatter())
    axe.tick_params(labelcolor='k',length=5)
    labels = axe.get_xticklabels() + axe.get_yticklabels()
    [label.set_fontproperties(FontProperties(fname="C:/WINDOWS/Fonts/times.ttf",size=8)) for label in labels]
    axe.set_title(title[i],loc='center',fontproperties=SimSun,fontsize=10,pad=2.5)
    if i!=0 and i!=4 and i!= 8 and i!=12:
        axe.set_yticklabels("")
    if i<12:
        axe.set_xticklabels("")
fig.subplots_adjust(bottom=0.23)
rect = [0.2, 0.14, 0.6, 0.015]
cbar_ax=fig.add_axes(rect)
cb = fig.colorbar(cf, cax=cbar_ax,shrink=0.6,orientation='horizontal',spacing='uniform')
cb.set_label('单位: $\mathrm{(ton/month)}$', fontproperties=SimSun, fontsize=10)
cb.ax.tick_params(length=0)  
plt.show()
bcfile.close()
出图如下
结果颜色是按照等间距填进去的,怎么解决啊

QQ图片20230910154258.png
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2023-9-11 09:46:14 | 显示全部楼层
norm = matplotlib.colors.BoundaryNorm(clevs_500_t , new_cmap.N)
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2023-9-12 10:29:18 | 显示全部楼层
用contourf中的levels=[]来设置不等间距的数据,想要自定义colorbar的话,可以直接从colors=color,color=[]#颜色列表,颜色的数量要比level的数量多一个  就可以绘制不等间距的填色图啦
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2023-9-19 22:20:59 | 显示全部楼层
安达充 发表于 2023-9-11 09:46
norm = matplotlib.colors.BoundaryNorm(clevs_500_t , new_cmap.N)

谢谢!终于解决了!
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表