patch for adding manual label location selection to clabel


Just because the discussion about clabel started, I want to post a
snipplet of code that I found useful. It was some sort of hack to get
nicer float formating for contours: contour lines represented
levels of 1, 2.5, 5 and 10 per cent, and I wanted a labeling exactly
I have written it here now. So, fmt='%.1f\%%' would have resulted in
1.0% 2.5% 5.0% ... but I wanted 1% 2.5% 5% ...

So this was my solution:

# some kind of hack: a nicer floating point formating
class nf(float):
     def __repr__(self):
         str = '%.1f' % (self.__float__(),)
         if str[-1]=='0':
             return '%.0f' % self.__float__()
             return '%.1f' % self.__float__()

levels = [nf(val) for val in [1.0, 2.5,5.0,10.0] ]

pylab.clabel(cs, inline=True, fmt='%r \%%')

As I said, it's sort of a hack but it works! It might not be worth to
add this to mpl, but probably as an example ...!?


Along these lines, I have been thinking that it would be a simple
addition to allow fmt to be a dictionary (as an alternative to a string)
that matches contour levels with the desired text. This would allow
users to label contours with arbitrary strings, which is occasionally
useful. If there is interest, I will add this feature.



On Thu, 2008-07-17 at 07:47 -0700, wrote:

David M. Kaplan
Charge de Recherche 1
Institut de Recherche pour le Developpement
Centre de Recherche Halieutique Mediterraneenne et Tropicale
av. Jean Monnet
B.P. 171
34203 Sete cedex

Phone: +33 (0)4 99 57 32 27
Fax: +33 (0)4 99 57 32 95