q1023
q1023

Reputation: 1

How to make pandas bar chart starting from 0?

df = pd.DataFrame({"hour": range(4,24,1), 
                   "rate": np.random.random(size = len(range(4,24,1)))
                  }, columns = ["hour", "rate"]).set_index("hour")
fig, ax = plt.subplots(1,1, figsize = (12, 8))

df.plot(kind = "bar", xlim = (0,24), ax = ax);

enter image description here

Need to have a bar for each hour, but xticks labels only aligns with the index.

Upvotes: 0

Views: 530

Answers (1)

James
James

Reputation: 36691

Why not just add the other values to the index?

df = pd.concat([pd.DataFrame([0]*4, columns=['rate']), df])
df.index.name = 'hour'

fig, ax = plt.subplots(1,1, figsize = (12, 8))
df.plot(kind = "bar", xlim = (0,24), ax=ax)

enter image description here

Upvotes: 1

Related Questions