0

Is it possible to define custom matpotlib boxstyles that look like these, eg. with a thick underline and a facecolor?

html text style example 1
html text style example 2

images from https://academy.datawrapper.de/article/190-how-to-turn-your-title-into-a-color-key

I managed the underline but if I only have this line as the returned path, facecolor won't have an effect. Is it possible to return two combined paths for example?

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

def underline(x0, y0, width, height, mutation_size, mutation_aspect=1, pad=0.3):
    """
    Given the location and size of the box, return the path of the box around
    it.

    Rotation is automatically taken care of.

    Parameters
    ----------
    x0, y0, width, height : float
        Box location and size.
    mutation_size : float
        Mutation reference scale, typically the text font size.
    mutation_aspect
        Mutation aspect ratio.
    """
    # We ignore mutation_aspect. This is okay in general.

    x1 = x0 + width
    # return the new path
    return Path([(x0, y0),
                 (x1, y0)],
                closed=False)

fig, ax = plt.subplots(figsize=(3, 3))
ax.text(0.5, 0.5, "Test", size=30, va="center", ha="center",
        bbox=dict(boxstyle=underline))
JohanC
  • 71,591
  • 8
  • 33
  • 66
znstrider
  • 15
  • 5
  • Probably the approaches of [Partial coloring of text in matplotlib](https://stackoverflow.com/questions/9169052/partial-coloring-of-text-in-matplotlib) can be extended. – JohanC Jan 27 '21 at 17:14
  • This is actually what I am intending on using this for. I have written the highlight-text package https://github.com/znstrider/highlight_text which I am currently refactoring and aiming on extending a bit. – znstrider Jan 27 '21 at 17:24

0 Answers0