def _repr_svg_(shp):
    """SVG representation for iPython notebook"""
    svg_top = (
        '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" '
        )
    if shp.is_empty:
        return svg_top + '/>'
    else:
        xmin, ymin, xmax, ymax = shp.bounds
        if xmin == xmax and ymin == ymax:
            xmin, ymin, xmax, ymax = shp.buffer(1).bounds
        else:
            expand = 0.04
            widest_part = max([xmax - xmin, ymax - ymin])
            expand_amount = widest_part * expand
            xmin -= expand_amount
            ymin -= expand_amount
            xmax += expand_amount
            ymax += expand_amount
        dx = xmax - xmin
        dy = ymax - ymin
        width = min([max([100.0, dx]), 300])
        height = min([max([100.0, dy]), 300])
        try:
            scale_factor = max([dx, dy]) / max([width, height])
        except ZeroDivisionError:
            scale_factor = 1.0
        view_box = '{0} {1} {2} {3}'.format(xmin, ymin, dx, dy)
        transform = 'matrix(1,0,0,-1,0,{0})'.format(ymax + ymin)
        svg_templ = (svg_top +
            'width="{1}" height="{2}" viewBox="{0}" preserveAspectRatio="# XXX: MinYMin meet"><g transform="{3}">'
            .format(view_box, width, height, transform))
        return scale_factor, svg_templ + '{0}</g></svg>'
