Я написал следующий код:
Код: Выделить всё
df = pd.read_csv(os.path.join(input_path, "data.csv"), sep=",")
category = df.columns[1]
items = df[df.columns[0]]
_, ax = plt.subplots(figsize=(10, 6))
x_coordinates = np.arange(len(items))
ax.plot(
x_coordinates,
list(df[category]),
label=category.title(),
)
ax.set_xlabel(items.name.title())
ax.set_ylabel(category.title())
ax.set_title("Test chart", wrap=True)
ax.set_xticks(x_coordinates, [str(item).title() for item in items])
y_coordinates = list(df[df.columns[1]])
x_coordinates = list(items)
for x_coordinate, y_coordinate in zip(x_coordinates, y_coordinates):
ax.text(
x_coordinate - 1,
y_coordinate,
y_coordinate, # This is the text that should be displayed
dict(zorder=10, size=10, color="black", ha="center"),
)
plt.savefig("output.png")
Код: Выделить всё
week,attendance
1,74788
2,74884
3,57677
4,74879
5,52632
6,74997
7,74292
8,49699
9,74908
10,74482
11,74379
12,42910
13,74186
14,37886
15,60038
[img]https://i.sstatic.net /ZLJ90UGm.png[/img]
Однако, учитывая следующие входные данные.csv:
Код: Выделить всё
year,purse
2001,60000
2002,60000
2003,60000
2004,70000
2005,70000
2006,85000
2007,100000
2008,100000
2009,110000
2010,120000
2011,120000
2012,100000
2013,100000

Я наблюдаю такое же поведение для других входных данных. У кого-то аннотации отображаются, у кого-то нет. Я не могу найти общий знаменатель, почему возникает проблема с входными данными. Есть ли в моем коде ошибка, которую я не замечаю?
Та же проблема возникает и при использовании ax.annotate (который также основан на ax.text):
Код: Выделить всё
ax.annotate(
value_label,
xy=(x_coordinate - 1, y_coordinate),
textcoords="offset points",
xytext=(0, 0),
zorder=10,
)
Буду очень признателен за помощь!
Я буду очень признателен за помощь!
п>
Подробнее здесь: https://stackoverflow.com/questions/791 ... ata-inputs
Мобильная версия