munch-group/geneinfo

Use matplotlib Rectangle patches for gene label backgrounds to allow more different background formats

kaspermunch opened this issue · 0 comments

Something like in the demo below. I would need to pass arguments to both text and Rectangle. I could add a backgroundpattern argument to gene_plot.

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

def hatches_plot(ax, h, color=None, hatchcolor=None):
    r = Rectangle((0, 0), 2, 1, fill=False, hatch=h, zorder=-1, alpha=0.3, edgecolor=hatchcolor)
    rx, ry = r.get_xy()
    cx = rx + r.get_width()/2.0
    cy = ry + r.get_height()/2.0
    ax.add_patch(r)
    ax.text(1, -0.5, f"' {h} '", size=15, ha="center")
    ax.text(cx, cy - 0.03, f"Gene", size=20, va="center", ha="center", color=color)
    ax.axis('equal')
    ax.axis('off')

fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2))
hatches = ['//', '/', '\\\\', '\\', '|', '-', '+', '++', 'x', 'xx']
for i, (ax, h) in enumerate(zip(axs.flat, hatches)):
    hatches_plot(ax, h, color=f'C{i}')