Hi All,

    I am trying to visualize some more text in an already rather
crowded 2D plot. As this new information I want to display is optional
(it depends on a use choice) but possibly very useful as information,
I would like to add it as text inside my plot, and possibly to add it
in a "smart" way (i.e., with minimum or no overlapping between this
new text and the rest of the plot, like legend does with loc=0).

Now, I have tried to steal the code from legend.py but I am not
getting anywhere, also because I am mixing display coordinates, data
coordinates and wx.DC text extents and I am getting crazy :-(

This is the code I have so far:


    def PositionAnnotation(self, dc, label, dash, x, y):
        """
        Position a matplotlib text instance without overlapping with
the rest of the data.

        :param `dc`: an instance of wx.ClientDC;
        :param `label`: the label to display (2 lines of text);
        :param `dash`: the actual TextWithDash matplotlib class to position
        :param `x`: a list of possible x location (this is fixed)
        :param `y`: a list of possible y location (this is fixed)
        """

        # Stolen from legend.py
        lines = []

       # Here I work with display coordinates (!)

        for handle in self.leftaxis.lines:
            path = handle.get_path()
            trans = handle.get_transform()
            tpath = trans.transform_path(path)
            lines.append(tpath)

        # End of stolen from legend.py

       # Here I work with screen/character coordinates (!)
        width, height, dummy = dc.GetMultiLineTextExtent(label)
        candidates = []

        for l, b in zip(x, y):

           # And here  I work with data coordinates (!)

            dashBox = Bbox.from_bounds(l, b, width+5, height+5)
            badness = 0
            for line in lines:
                if line.intersects_bbox(dashBox):
                    badness += 1

            ox, oy = l, b
            if badness == 0:
                return ox, oy

            candidates.append((badness, (l, b)))

        # Stolen from legend.py

        # rather than use min() or list.sort(), do this so that we are assured
        # that in the case of two equal badnesses, the one first considered is
        # returned.
        # NOTE: list.sort() is stable.But leave as it is for now. -JJL
        minCandidate = candidates[0]
        for candidate in candidates:
            if candidate[0] < minCandidate[0]:
                minCandidate = candidate

        ox, oy = minCandidate[1]

        return ox, oy


This code is not doing anything useful as I always get a badness of 0,
although I can see that the new text overlaps quite a lot of other
artists.

Does anyone have some suggestion on how to improve the code?

Thank you in advance.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/
http://thedoomedcity.blogspot.com/

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to