Matplotlib Contour Clabel Location Matplotlib Contour Clabel Location python python

Matplotlib Contour Clabel Location


Yes, there now is a way to control label locations!https://github.com/matplotlib/matplotlib/pull/642

plt.figure()CS = plt.contour(X, Y, Z)manual_locations = [(-1, -1.4), (-0.62, -0.7), (-2, 0.5), (1.7, 1.2), (2.0, 1.4), (2.4, 1.7)]plt.clabel(CS, inline=1, fontsize=10, manual=manual_locations)


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    passar = np.array([[1,0], [0,1]]c = matplotlib.contour(ar)c.locate_label = your_locate_labelc.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