Using matplotlib in python I have the issue, that for specific inputs axis annotation and axis labels are not shown.
I wrote the following code:
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")
Let's look at this input data.csv:
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
The text/annotations are plotted correctly:

However, given the following input data.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
The following chart is plotted:

I am observing the same behavior for other data inputs. For some the annotations are displayed, for some not. I can not find a common denominator as to why the issue occurs with the input data. Is there a fault in my code which I am overlooking?
The same issue also occurs using ax.annotate (which also builds on top of ax.text):
ax.annotate(
value_label,
xy=(x_coordinate - 1, y_coordinate),
textcoords="offset points",
xytext=(0, 0),
zorder=10,
)
I tried debugging the issue, but all the input values to .text/.annotate are correct.
Help would be greatly appreciated!


