Phlya / adjustText

A small library for automatically adjustment of text position in matplotlib plots to minimize overlaps.
https://adjusttext.readthedocs.io/
MIT License
1.5k stars 87 forks source link

adjust_text doesn't like categorical X data #153

Closed owendaily closed 1 year ago

owendaily commented 1 year ago

the get_text_position uses ax.convert_xunits which seems to return either a float for numerical data or an ndarray containing the category index for categorical. this raises the following error when trying to apply a transformation to the text: ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.

currently using the following code as a test:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from adjustText import adjust_text

data = pd.DataFrame({
    'x': ["Cat", "Dog", "Bird"]*3,
    'y': [0, 0, 0, 20, 20, 20, 0.5, 19.5, 19.5],
    'label': [x%2==0 for x in np.random.randint(0, 20, 9)]
})

fig = plt.figure()
ax = fig.add_subplot()
ax.scatter(x=data['x'], y=data['y'])
annotations = [ax.annotate(text=t, xy=(x,y)) for x,y,t in zip(data['x'], data['y'], data['label'])]
adjust_text(annotations)

current figure (doesn't run adjusttext):

image
owendaily commented 1 year ago

oh nvm lol, looks like this was fixed, but not released, sorry !