Pyplot contour plot - clabel spacing

*爱你&永不变心* 提交于 2019-12-04 11:45:24

问题


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.


回答1:


The "inline_spacing" parameter can be set to negative values. It gave me a warning, but trying -2 or -3 should probably fix your problem.




回答2:


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!



来源:https://stackoverflow.com/questions/27219621/pyplot-contour-plot-clabel-spacing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!