Skip to content

Problem with passing a PathCollection object as the objects argument #184

@the-pawel-wojcik

Description

@the-pawel-wojcik

Hello,

A very helpful tool! All works great when I work with text objects alone. I run into troubles after asking for a bit more complex task.

I would like to use adjust_text to avoid a set of lines that I draw using vlines. When I take the output and turn it into PathCollection object I run into errors. It could be that I just don't know how to use PathCollection.

I am using matplotlib 3.9.2 and python 3.12.3.

An example:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from adjustText import adjust_text

# prepare input
npeaks = 10
xs = np.linspace(0, 1, npeaks)
rng = np.random.default_rng(seed=1)
heights = rng.random(size=npeaks)

# plot stems
fig, ax = plt.subplots(figsize=(3, 3))
vlines: mpl.collections.LineCollection = ax.vlines(xs, heights, 0.0)

# add texts
texts = list()
for x, y in zip(xs, heights):
    text = ax.text(x, y/2, "note", ha='center', va='center')
    texts += [text]

# turn the LineCollection into a PathCollection
path_list: list[mpl.path.Path] = vlines.get_paths()
print(f"{path_list[0]=}")
pathcoll = mpl.collections.PathCollection(path_list)
print(f"{pathcoll=}")

adjust_text(texts, objects=pathcoll)

plt.show()

Thanks
Pawel

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