python等值线如何设置高度的范围和间隔_Pyplot等高线图-clabel间距

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-22 16:26   24   0

I have trouble with matplotlib / pyplot / basemap. I plot contour lines (air pressure) on a map. I use clabel to show the value of the contour lines.

But the problem is: the padding between the value and the contour line is too much. I have found the parameter "inline_spacing", which i have set to zero. But there is still to much free space. Any ideas?

Python Code:

import matplotlib

matplotlib.use('Agg')

import matplotlib.pyplot as plt

from mpl_toolkits.basemap import Basemap

import pygrib

filename = "file.grib2"

grbs = pygrib.open('/data/' + filename)

grb = grbs[2]

data = grb.values

datac = data*0.01

lats, lons = grb.latlons()

fig = plt.figure()

m = Basemap(projection='stere',lon_0=5,lat_0=90.0,\

llcrnrlon=-25.0,urcrnrlon=60.0,llcrnrlat=30.0,urcrnrlat=60.0,resolution='l')

x, y = m(lons, lats)

levs = range(940,1065,5)

S1=plt.contour(x,y,datac,levs,linewidths=0.5,colors='b')

plt.clabel(S1,inline=1,inline_spacing=0,fontsize=8,fmt='%1.0f',colors='b')

m.drawmapboundary(fill_color='w')

m.drawcoastlines(linewidth=0.2)

plt.savefig('test.png', bbox_inches='tight',pad_inches=0.05, dpi=100)

Thanks.

解决方案cb = plt.clabel(S1,inline=1,inline_spacing=0,fontsize=8,fmt='%1.0f',colors='b')

[txt.set_bbox(dict(boxstyle='square,pad=0',fc='red')) for txt in cb]

Matplotlib Text class create a bbox. You need to set the pad = 0.Then inline_spacing works.

Refer to the Question!

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP