Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
the-pawel-wojcik opened this issue Oct 17, 2024 · 0 comments

Comments

@the-pawel-wojcik
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant