Yavor I
Yavor I

Reputation: 87

Pyplot stacked bars with dates

First of all - sorry for the too general question, but I didn't find good example of the code that I am looking for.

I have a DF similar to:

Customer Size Date
Customer1 5 01.01.2021
Customer2 2 01.01.2021
Customer3 6 01.01.2021
Customer1 5 02.01.2021
Customer2 4 02.01.2021
Customer3 5 02.01.2021

Basically I have some customers using specific amount of storage per day. I need a stacked bar plot, where each bar is a single day, and the sections of each bar are the customers. I plan to have 30 days history, placed in a single chart, which means 30 bars with their X number of sections.

No need to have visual space between the bars.

Thank you!

Upvotes: 1

Views: 1357

Answers (1)

Corralien
Corralien

Reputation: 120559

Transform your dataframe before plot as suggested by @ChrisAdams:

out = df.pivot('Date', 'Customer', 'Size')
print(out)

# Output:
Customer    Customer1  Customer2  Customer3
Date                                       
01.01.2021          5          2          6
02.01.2021          5          4          5

Now you can plot your dataframe:

out.plot.bar(stacked=True, rot=0)
plt.show()

enter image description here

Update

I am asking this, because I want to change figure parameters, and can't find a way to combine your syntax, with the fig, ax=... way.

fig, ax = plt.subplots()
out.plot.bar(stacked=True, rot=0, ax=ax)

Upvotes: 1

Related Questions