1

I am implementing a line label class using matplotlib similar to the idea mentioned here.

I have subclassed matpltotlib.text.Text and in principle it is working fine. The problem is hiding the line under the label. I have tried two approaches for this so far.

  1. As in the linked thread, one can simply give the text element a white bounding box. This works fine as long as labels don't get close to each other. When they are too close the boxes start covering each other or the other lines.

  2. What I am currently doing is setting the ydata of the corresponding Line2D object to NaN in the region where it is covered by the label. This is also suboptimal as it only works when there are a lot of points in the line. I would however also like for it to work with less intermediate points.

So here is my question:

Is it somehow possible to blank out sections of lines in matplotlib? I have experimented with clipboxes for lines, but did not succeed. So what can I do if I for example plot a line with only the end points like so:

plt.plot([0,1],[0,1])

and want the line only to be shown outside a certain region, e.g. a box in with [left,bottom,width,height] = [0.4,0.4,0.2,0.2]?

Community
  • 1
  • 1
thomas
  • 1,773
  • 10
  • 14
  • Your first attempt sounds ok, try removing the padding of the bounding box to a minimum. What do you want to do when the labels touch each other? how would they not cover each other? – Elad Joseph Nov 25 '15 at 18:49
  • If I make the padding too small, the distance between the line and the string can look too small. I often use Tex equations as the label. This can make the bounding box unnecessarily large. Sometimes labels where the bounding boxes would intersect still look ok. Also, adding white patches just feels like something you would do in PowerPoint. I looked at the implementation of Contour, but don't really get it. It seems they use paths for the contours. – thomas Nov 25 '15 at 18:58

0 Answers0