Pandas Series bar_label() values off by 1

Question:

I am trying to plot a bar chart from a Pandas series using the inbuilt .plot() attribute, and the values in the bar chart are 1 more than the values in the series. See the code snippet below:

import pandas as pd
import numpy as np

data = np.array([10000, 1000, 100, 10, 5, 1])
 
ser = pd.Series(data, index=[1, 2, 3, 4, 5, 6])

print(ser)

ax = ser.sort_index().plot(log=True, kind="bar")

for container in ax.containers:
    ax.bar_label(container)

Here is the output and the generated plot:

1    10000
2     1000
3      100
4       10
5        5
6        1
dtype: int64

enter image description here

As you can see, the value annotations in the bar chart are off by 1. What could be causing this?

Asked By: LeviAckerman

||

Answers:

That’s probably because the log=True param sets the lower y limit to 10**0 = 1. Remove it and set the log scale manually helps set the correct labels.

ax = ser.sort_index().plot(kind="bar")

for container in ax.containers:
    ax.bar_label(container)
    
plt.yscale('log')

Output:

enter image description here

I’d say this is a bug on Pandas side.

Answered By: Quang Hoang
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.