views:

193

answers:

1

I would like to control the location of matplotlib clabels on a contour plot, but without utilizing the manual=True flag in clabel. For example, I would like to specify an x-coordinate, and have labels created at the points that pass through this line. I see that you can get the location of the individual labels using get_position(), but I am stuck at that. Any help would be greatly appreciated. Thanks!

EDIT: alt text

The above image is an example plot that I would like to apply this method to. The default label positions are inconvenient -- the flat areas between Day 2 and Day 4 would be more visually appealing.

+2  A: 

No, there is no way built into matplotlib to do that. You are supposed to either live with the default locations or go fully interactive with manual and using the mouse.

You might want to file this as a bug report upstream so they can improve their algorithms.

There are multiple options to work around this. The first one is to programmatically place text on the contour figure. You will not be able to reliably remove the lines underneath the text this way. Assuming you have a contour c you can find the contour lines in c.collections. For every contour line invoke get_paths and place your text on that path.

The other option would be to replace the code for manual placement (in matplotlib.contour.BlockingContourLabeler) or tweak the code that finds the label positions (in matplotlib.contour.locate_label), but both functions are pretty dense. If you can come up with a working replacement for locate_label just overwrite the old method in your plotting macro

def your_locate_label(self, linecontour, labelwidth):
    # some magic
    pass

ar = np.array([[1,0], [0,1]]
c = matplotlib.contour(ar)
c.locate_label = your_locate_label

c.clabel()

Btw, if you use ipython you can easily view the function source from your interactive session with

%psource c.clabel

or directly invoke your $EDITOR on the file were it is defined with

%edit c.clabel
honk