Kai-Wei Chang
Kai-Wei Chang

Reputation: 41

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?

Upvotes: 4

Views: 156

Answers (2)

ranger
ranger

Reputation: 37

You can replace

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

with

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

Upvotes: 0

Patol75
Patol75

Reputation: 4547

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() ?

Upvotes: 1

Related Questions