Commit fc245bba authored by Michele Maris's avatar Michele Maris
Browse files

U

parent fe92d1c1
Loading
Loading
Loading
Loading
+100 −0
Original line number Diff line number Diff line
from matplotlib import pyplot as plt
import matplotlib.patches as patches

def draw_text_bbox(
    x, y, text, 
    ax=None,
    fontsize=18, 
    pad=0.5, 
    facecolor='yellow', 
    edgecolor='red', 
    text_color='blue',
    linewidth=2,
    zorder=4,
    boxshape='circle',
    ha='center',
    va='center'
):
    """
    At a given position x,y 
    
    if ax is not given assumes current axis
    
    Draws a perfect circle or other box shape (boxshape) around text regardless of axis aspect ratio.

    `pad` controls how much larger the circle background is relative to the text.

    boxshape for bbox (default cirlce) are:
    
    round: A rectangle with rounded corners.

        Parameters: pad (default 0.3), rounding_size (default 0.2)

        Example: boxstyle="round,pad=0.4,rounding_size=0.2"

    round4: A rectangle with four curved edges (pill or rounded bulge shape).

        Parameters: pad (default 0.3), rounding_size (default 0.2)

        Example: boxstyle="round4,pad=0.5,rounding_size=0.3"

    sawtooth: A box with sharp, zig-zag / sawtooth border edges.

        Parameters: pad (default 0.3), tooth_size (default 0.1)

        Example: boxstyle="sawtooth,pad=0.5,tooth_size=0.2"

    roundtooth: A box with wavy, rounded sawtooth border edges.

        Parameters: pad (default 0.3), tooth_size (default 0.1)

        Example: boxstyle="roundtooth,pad=0.5,tooth_size=0.2"

    larrow: A box shaped like a left-pointing arrow.

        Parameters: pad (default 0.3)

        Example: boxstyle="larrow,pad=0.5"

    rarrow: A box shaped like a right-pointing arrow.

        Parameters: pad (default 0.3)

        Example: boxstyle="rarrow,pad=0.5"

    darrow: A box shaped like a double-ended (left & right) horizontal arrow.

        Parameters: pad (default 0.3)

        Example: boxstyle="darrow,pad=0.5"

    square: Standard rectangular box with sharp 90-degree corners.

        Parameters: pad (default 0.3)

        Example: boxstyle="square,pad=0.5"

    circle: A perfectly round box scaled around the text.

        Parameters: pad (default 0.3)

        Example: boxstyle="circle,pad=0.5"
    """
    if ax is None :
       _ax=plt.gca()
    else :
       _ax=ax
    ax.text(
        x, y, text,
        fontsize=fontsize,
        color=text_color,
        ha=ha,
        va=va,
        zorder=zorder,
        bbox=dict(
            boxstyle=f'{boxshape},pad={pad}',
            facecolor=facecolor,
            edgecolor=edgecolor,
            linewidth=linewidth
        )
    )