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

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

我有以下代码: 资料来源: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()

确实有阴谋,但我一直收到这个错误:

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

你能告诉我为什么会出现这个错误吗?

完整的错误回溯:

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'

出现此错误是因为,您提供的 only_move 是一个字符串,但它需要一个字典。在错误屏幕截图的最后一行中,您可以看到 only_move.values(),从那里弹出错误。 PFB 示例:

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

供您参考: https://adjusttext.readthedocs.io/en/latest/Examples.html