Home > OS >  How to plot timeseries bar chart with multiple values per stack/ timestamp from pandas dataframe
How to plot timeseries bar chart with multiple values per stack/ timestamp from pandas dataframe

Time:11-16

                               value      cumsum   price
0    2021-02-01 00:00:00    164.6136    164.6136  0.0216
     2021-02-01 00:00:00    163.8085    328.4221  0.0215
     2021-02-01 00:00:00    163.0466    491.4687  0.0214
     2021-02-01 00:00:00  14999.9925  15491.4612  0.0213
1    2021-02-01 00:00:10    164.6136    164.6136  0.0216
...                              ...         ...     ...
8634 2021-02-01 23:59:00  14999.9993  14999.9993  0.0221
8635 2021-02-01 23:59:10  14999.9993  14999.9993  0.0221
8636 2021-02-01 23:59:20  14999.9993  14999.9993  0.0221
8637 2021-02-01 23:59:30      0.0000      0.0000  0.0221
     2021-02-01 23:59:30  14999.9993  14999.9993  0.0221

My data looks like the above, and I would like to plot a graph like below enter image description here

Can somebody please help me?

CodePudding user response:

You can use the below code to plot the graph.

If the code helps you, accept it as answer and upvote it pls.

import matplotlib.pyplot as plt
import pandas as pd
 
# create data
df = pd.DataFrame([['A', 10, 20, 10, 26],['A', 10, 40, 10, 26],['A', 10, 70, 10, 26], ['B', 20, 25, 15, 21],['B', 20, 45, 15, 21], ['C', 12, 15, 19, 6],
                   ['D', 10, 18, 11, 19]],
                  columns=['Team', 'Round 1', 'Round 2', 'Round 3', 'Round 4'])
# view data
df

# # plot data in stack manner of bar type
df.pivot("Team", "Round 2").plot(kind='bar',stacked=True).legend().set_visible(False)


plt.show()
df

This is what you get as output. Do you expect this?

enter image description here

  • Related