Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyplot contour plot - clabel spacing

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?

free space contour label

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.

like image 405
mcatis Avatar asked Nov 30 '14 23:11

mcatis


2 Answers

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.

like image 149
David Manheim Avatar answered Oct 29 '22 05:10

David Manheim


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!

like image 33
Li Ziming Avatar answered Oct 29 '22 04:10

Li Ziming