Skip to content

bug[?]: adjustText doesn't handle FancyArrows correctly #200

@pank

Description

@pank

I am using networkx and mpl for making graphical representations of graphs. (I can't use graphviz for this project). adjustText seems like a perfect match for networkx as it doesn't adjust labels, see for example this old example where some labels overlap other artists.

My networkx figures use FancyArrowPatch for connecting nodes (not sure if this is always the case; I'm new to networkx). These don't seem to work well with adjustText, presumably because they don't have a get_bbox method.

I will submit a patch addressing this problem that maybe could be added if it passes mustard.

Example:

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

from adjustText import adjust_text

t_config = {"s": "I won't be nudged", "ha": "center"}

fig, ax = plt.subplots(figsize=(3,3))
ax.add_patch(FancyArrowPatch((0, 0), (10, 10)))
t = ax.text(5, 5, **t_config)
original_loc = t.get_position()
ax.autoscale()

# fig.draw_without_rendering()  # not sure if this is needed but just to make sure...
adjust_text([t], objects=[*ax.patches], avoid_self=False, ax=ax)

ax.annotate("(current adjusttext location)", xy=(.5, 0),
            xycoords=t, ha="center", va="top", color="red", fontsize=8)


t_ideal = ax.text(3, 6.5, **t_config, color="C1")
ax.annotate("(ideal-ish location)", xy=(.5, 1),
            xycoords=t_ideal, ha="center", va="bottom", color="red", fontsize=8)

t_bbox_approach = ax.text(10, 10, **t_config, color="C2")
ax.annotate("(bbox location) ", xy=(0, .5),
            xycoords=t_bbox_approach, ha="right", va="center", color="red", fontsize=8)

print(f"Label position: {t.get_position()}")
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions