Home > Blockchain >  Pyplot stacked bars with dates
Pyplot stacked bars with dates

Time:12-17

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!

CodePudding user response:

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

  • Related