HTML area for a matplotlib picture

Hello,

This code worked with matplotlib 0.87:

    fig = Figure()
    ax = fig.add_subplot(121)
    width = 0.5 # the width of the bars
    self.dpi = 70
    yoff = array([0.0] * len(self.__labels)) # the bottom values for stacked bar chart

    self.__bars = ()
    for row in xrange(rows):
      self.__bars += (ax.bar(ind, self.__data[row], width, bottom=yoff, color=colours),)
      yoff = yoff + self.__data[row]
    
    #...
    fig.set_size_inches(self.size[0], self.size[1])
    imdata=StringIO()
    fig.savefig(imdata,format='png', dpi=self.dpi)
    self.__image = imdata.getvalue()
    self.height = fig.get_figheight()* self.dpi
    #...
    coords = []
    
    for bar in self.__bars:
        x1 = []
        y1 = []
        x2 = []
        y2 = []
        # For each bars, we get values of rectangles
        for b in bar:
            x1 += [b.get_x(),]
            y1 += [b.get_y(),]
            if matplotlib.compare_versions(matplotlib.__version__, '0.97'):
                x2 += [b.get_x() + b.get_width(),]
                y2 += [b.get_y() + b.get_height(),]
            else:
                x2 += [b.get_x() + b.width,]
                y2 += [b.get_y() + b.height,]
    
        if matplotlib.compare_versions(matplotlib.__version__, '0.97'):
            xys1 = zip(x1, y1)
            xys2 = zip(x2, y2)
            # We get the top left points for each bars...
            xys1 = bar[0].get_transform().transform(xys1)
            # ...and the bottom right points for each bars.
            xys2 = bar[0].get_transform().transform(xys2)
        else:
            xys1 = zip(bar[0].get_transform().seq_x_y(x1, y1))
            xys2 = zip(bar[0].get_transform().seq_x_y(x2, y2))
    
        # Now for each bars, we can make a (x,y,w,h) data coords for HTML map area.
        for (sx1, sy1), (sx2, sy2) in zip(xys1, xys2):
            coords.append(sx1, self.height - sy2, sx2, self.height))
    #...then coords is used to build the html area tags

Where it works with matplotlib 0.87, the behavior isn't correct with matplotlib 0.98.

There are some version tests to run correctly compatible code. Blocks are
equivalents, but as the v0.87 code works, the v0.98 code doesn't produce the
same behavior.

For example, if xys1 and xys2 are:
xys1 = [(0.0, 0.0), (0.5, 0.0), (1.0, 0.0), (1.5, 0.0), (2.0, 0.0), (2.5, 0.0), (3.0, 0.0), (3.5, 0.0), (4.0, 0.0), (4.5, 0.0)]
xys2 = [(0.5, 992.0), (1.0, 1025.0), (1.5, 972.0), (2.0, 1004.0), (2.5, 1007.0), (3.0, 1004.0), (3.5, 974.0), (4.0, 995.0), (4.5, 981.0), (5.0, 964.0)]

The arrays produced by the transform() methods will be:

xys1 = [[ 50. 20. ]
        [ 65.5 20. ]
        [ 81. 20. ]
        [ 96.5 20. ]
        [ 112. 20. ]
        [ 127.5 20. ]
        [ 143. 20. ]
        [ 158.5 20. ]
        [ 174. 20. ]
        [ 189.5 20. ]]
xys2 = [[ 6.55000000e+01 1.31228533e+05]
        [ 8.10000000e+01 1.35593333e+05]
        [ 9.65000000e+01 1.28583200e+05]
        [ 1.12000000e+02 1.32815733e+05]
        [ 1.27500000e+02 1.33212533e+05]
        [ 1.43000000e+02 1.32815733e+05]
        [ 1.58500000e+02 1.28847733e+05]
        [ 1.74000000e+02 1.31625333e+05]
        [ 1.89500000e+02 1.29773600e+05]
        [ 2.05000000e+02 1.27525067e+05]]

Firstly, the horizontal axe is separated with 15.5 points, but on picture, 28
are needed.
Then, vertical values are around 1.0e+05, but self.height=175!

Is there any matplotlib behavior changed in new versions?

Thanks, regards

···

--
Romain Bignon -- http://romain.peerfuse.org

http://peerfuse.org