简体   繁体   中英

Distorted plot from using clabel with contourf in Python

I have a image from contourf that looks like this:

It is made from this code snippet:

fig = plt.figure()
Z = uavg_lon[:,1,:].transpose()
plt.title('ERA-interim 100hpa U average in [-20,20]',fontsize=16)
c0 = plt.contourf( lon , range(4*12) , Z , cmap = cm.bwr , levels = 
range(-25,30,5))
#plt.clabel(c0, fmt='%2d', inline=True)
plt.xlim([-180,180])
plt.xlabel('longitude',fontsize=14)
plt.xticks(np.arange(-180,180+30,30),fontsize=14)
plt.yticks(monthlabellocs,monthlabels,fontsize=14)
plt.ylim([0,48])
plt.xlim([-180,180])
fig.set_size_inches(8,10)
fn = 'hovmol_100u.pdf'
fig.savefig(fn , format='pdf' , bbox_inches='tight')

When I try to add contour labels with the clabel (by uncommenting the commented line), I get this:

I cannot find any examples online that describe this problem. What is happening, and how can I fix this?

I may be wrong, but I think it is not the best idea to call clabel() on an object returned by contourf(). Documentation says

Adds labels to line contours in cs, where cs is a ContourSet object returned by contour().

Can you instead try to call contour() after contourf() for the specific contours you would like and then call clabel() on the object returned by contour() ?

You can replace

plt.clabel(c0, fmt='%2d', inline=True)

with

plt.clabel(c0, fmt='%2d', inline=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM