Search code examples
pythonnumpymatplotlibannotations

AttributeError: 'str' object has no attribute 'values'


I have the following code: Source: Matplotlib overlapping annotations / text

import matplotlib.pyplot as plt
from adjustText import adjust_text
import numpy as np
together = [(0, 1.0, 0.4), (25, 1.0127692669427917, 0.41), (50, 1.016404709797609, 0.41), (75, 1.1043426359673716, 0.42), (100, 1.1610446924342996, 0.44), (125, 1.1685687930691457, 0.43), (150, 1.3486407784550272, 0.45), (250, 1.4013999168008104, 0.45)]
together.sort()

text = [x for (x,y,z) in together]
eucs = [y for (x,y,z) in together]
covers = [z for (x,y,z) in together]

p1 = plt.plot(eucs,covers,color="black", alpha=0.5)
texts = []
for x, y, s in zip(eucs, covers, text):
    texts.append(plt.text(x, y, s))

plt.xlabel("Proportional Euclidean Distance")
plt.ylabel("Percentage Timewindows Attended")
plt.title("Test plot")
adjust_text(texts, only_move='y', arrowprops=dict(arrowstyle="->", color='r', lw=0.5))
plt.show()

It does plot but i keep getting this error:

AttributeError: 'str' object has no attribute 'values'

Could you please advise why am i getting this error?

Full Error Traceback:

runfile('C:/Users//untitled3.py', wdir='C:/Users/')
Traceback (most recent call last):

  File "C:\Users\\untitled3.py", line 27, in <module>
    adjust_text(texts, only_move='y', arrowprops=dict(arrowstyle="->", color='r', lw=0.5))

  File "C:\Users\\anaconda3\lib\site-packages\adjustText\__init__.py", line 439, in adjust_text
    if not any(list(map(lambda val: 'x' in val, only_move.values()))):

AttributeError: 'str' object has no attribute 'values'

Solution

  • This error popped up because, the only_move you have provided is a string, but it expects a dictionary. In the last line of your error screenshot you can see only_move.values(), from where the error pops up. PFB example:

    adjust_text(texts, x, y, arrowprops=dict(arrowstyle="->", color='r', lw=0.5),
            autoalign='', only_move={'points':'y', 'text':'y'})
    

    For your reference: https://adjusttext.readthedocs.io/en/latest/Examples.html