.. note::
    :class: sphx-glr-download-link-note

    Click :ref:`here <sphx_glr_download_gallery_userdemo_custom_boxstyle02.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_userdemo_custom_boxstyle02.py:


=================
Custom Boxstyle02
=================





.. image:: /gallery/userdemo/images/sphx_glr_custom_boxstyle02_001.png
    :class: sphx-glr-single-img





.. code-block:: python

    from matplotlib.path import Path
    from matplotlib.patches import BoxStyle
    import matplotlib.pyplot as plt


    # we may derive from matplotlib.patches.BoxStyle._Base class.
    # You need to override transmute method in this case.
    class MyStyle(BoxStyle._Base):
        """
        A simple box.
        """

        def __init__(self, pad=0.3):
            """
            The arguments need to be floating numbers and need to have
            default values.

             *pad*
                amount of padding
            """

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

        def transmute(self, x0, y0, width, height, mutation_size):
            """
            Given the location and size of the box, return the path of
            the box around it.

             - *x0*, *y0*, *width*, *height* : location and size of the box
             - *mutation_size* : a reference scale for the mutation.

            Often, the *mutation_size* is the font size of the text.
            You don't need to worry about the rotation as it is
            automatically taken care of.
            """

            # padding
            pad = mutation_size * self.pad

            # width and height with padding added.
            width, height = width + 2.*pad, \
                            height + 2.*pad,

            # boundary of the padded box
            x0, y0 = x0-pad, y0-pad,
            x1, y1 = x0+width, y0 + height

            cp = [(x0, y0),
                  (x1, y0), (x1, y1), (x0, y1),
                  (x0-pad, (y0+y1)/2.), (x0, y0),
                  (x0, y0)]

            com = [Path.MOVETO,
                   Path.LINETO, Path.LINETO, Path.LINETO,
                   Path.LINETO, Path.LINETO,
                   Path.CLOSEPOLY]

            path = Path(cp, com)

            return path


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

    fig, ax = plt.subplots(figsize=(3, 3))
    ax.text(0.5, 0.5, "Test", size=30, va="center", ha="center", rotation=30,
            bbox=dict(boxstyle="angled,pad=0.5", alpha=0.2))

    del BoxStyle._style_list["angled"]

    plt.show()


.. _sphx_glr_download_gallery_userdemo_custom_boxstyle02.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download

     :download:`Download Python source code: custom_boxstyle02.py <custom_boxstyle02.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: custom_boxstyle02.ipynb <custom_boxstyle02.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
