1

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:

correct annotation

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:

missing annotation

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!

1 Answer 1

0

TL;DR the usage of x_coordinates is not right. The first time it worked because the numbers were going from 1->15. Which means that x_coordinates = np.arange(len(items)) would work only if the values are indeed equal to the sample number. If you zoom to x 2000-2014 in your second example, you will find the annotations (check image below).

Problem

Additionally, your code has some redundant lines and conversions.

  • list(df[category]) is not needed
  • y_coordinates = list(df[df.columns[1]]) and x_coordinates = list(items) are also not needed
  • I also don't understand the reason behind the usage of category = df.columns[1] and items = df[df.columns[0]]

Here's a more efficient way, do ask anything if something is not clear from the comments:

df = pd.read_csv("data.csv", index_col=0) # set the first column as an index
plt.plot(df) # plot the df with respect to its' index
plt.xlabel(df.index.name) # set xlabel
plt.ylabel(df.columns[0]) # and y label
for x, y in zip(df.index, df.iloc[:,0]): # annotate values, here you can also play arround with skipping rows...
    plt.text(x, y, f"{y}", ha="center", va="bottom", fontsize=10, color="black")

For first dataset:

first

And the second one:

second

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.