0

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'
Slartibartfast
  • 1,058
  • 4
  • 26
  • 60
  • Please add the **full** error traceback formatted as code to your question! – Klaus D. Jul 05 '20 at 18:49
  • @KlausD. I have updated the question with full error traceback as requested. – Slartibartfast Jul 05 '20 at 18:52
  • 1
    It looks like `only_move` should be a dictionary instead of a string. – Klaus D. Jul 05 '20 at 18:55
  • 1
    The problem is in the parameters expected by `adjust_text()`. You pass in a string `only_move='y'` but the function expects something else. Since we don't have details on what this function is, its not something we can debug. Read this function's documentation to see what that parameter should be. – tdelaney Jul 05 '20 at 18:56
  • 2
    only_move (dict, default {‘points’:’xy’, ‘text’:’xy’, ‘objects’:’xy’}) – a dict to restrict movement of texts to only certain axes for certain types of overlaps. Valid keys are ‘points’, ‘text’, and ‘objects’. Valid values are ‘’, ‘x’, ‘y’, and ‘xy’. For example, only_move={‘points’:’y’, ‘text’:’xy’, ‘objects’:’xy’} forbids moving texts along the x axis due to overlaps with points. - From https://adjusttext.readthedocs.io/en/latest/ – alani Jul 05 '20 at 18:56

1 Answers1

2

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

Aparna
  • 422
  • 2
  • 5