from matplotlib.path import Path
from matplotlib.patches import BoxStyle

class MyStyle(BoxStyle._Base):
    """
    A simple box.
    """

    def __init__(self, pad=0.3):

        self.pad = pad
        super(MyStyle, self).__init__()

    def transmute(self, x0, y0, width, height, mutation_size):

        # padding
        pad = mutation_size * self.pad

        cx, cy = x0+.5*width, y0+.5*height # center
        
        # width and height with padding added.
        width, height = width + 2.*pad, height + 2.*pad,

        # get radius
        radius = (width**2 + height**2)**.5 * .5

        cir_path = Path.unit_circle()
        vertices = radius*cir_path.vertices + (cx, cy)

        # a path of the circle
        path = Path(vertices, cir_path.codes)

        return path


# register the custom style
BoxStyle._style_list["circle"] = MyStyle

if __name__ == '__main__':
    
    import matplotlib.pyplot as plt
    plt.figure(1, figsize=(3,3))
    ax = plt.subplot(111)
    ax.text(0.5, 0.5, "a", size=30, va="center", ha="center",
            bbox=dict(boxstyle="circle,pad=0.", alpha=0.2))


    plt.show()
