Home > database >  plotting stacked bar graph
plotting stacked bar graph

Time:11-17

i want to plot stacked bar graph using matplotlib and pandas.The below code plot the bargraph very nicely.However when i change a,b,c,d,e,f,g,h ..etc to January, February... it doesnot plot the same graph.It only plot alphabetical order.Are there anyway to overcome this problem.

import pandas as pd
import matplotlib.pyplot as plt

years=["2016","2017","2018","2019","2020", "2021"]
dataavail={
    "a":[20,0,0,0,10,21],
    "b":[20,13,10,18,15,45],
    "c":[20,20,10,15,18,78],
    "d":[20,20,10,15,18,75],
    "e":[20,20,10,15,18,78],
    "f":[20,20,10,15,18,78],
    "g":[20,20,10,15,18,78],
    "h":[20,20,10,15,18,78],
    "i":[20,20,10,15,18,78],
    "j":[20,20,10,15,18,78],
    "k":[20,20,10,15,18,78],
    "l":[20,20,0,0,0,20],
}


df=pd.DataFrame(dataavail,index=years)

df.plot(kind="bar",stacked=True,figsize=(10,8))
plt.legend(loc="centre",bbox_to_anchor=(0.8,1.0))
plt.show()

But when i change the code in the portion a,b,c...to January,February... it doesnot plot the same.

import pandas as pd import matplotlib.pyplot as plt

years=["2016","2017","2018","2019","2020", "2021"]
dataavail={
    "january":[20,0,0,0,10,21],
    "February":[20,13,10,18,15,45],
    "March":[20,20,10,15,18,78],
    "April":[20,20,10,15,18,75],
    "may":[20,20,10,15,18,78],
    "June":[20,20,10,15,18,78],
    "July":[20,20,10,15,18,78],
    "August":[20,20,10,15,18,78],
    "September":[20,20,10,15,18,78],
    "October":[20,20,10,15,18,78],
    "November":[20,20,10,15,18,78],
    "December":[20,20,0,0,0,20],
}


df=pd.DataFrame(dataavail,index=years)

df.plot(kind="bar",stacked=True,figsize=(10,8))
plt.legend(loc="centre",bbox_to_anchor=(0.8,1.0))
plt.show()

CodePudding user response:

With Python 3.9.7, your graphs look like the same:

>>> df_alpha
       a   b   c   d   e   f   g   h   i   j   k   l
2016  20  20  20  20  20  20  20  20  20  20  20  20
2017   0  13  20  20  20  20  20  20  20  20  20  20
2018   0  10  10  10  10  10  10  10  10  10  10   0
2019   0  18  15  15  15  15  15  15  15  15  15   0
2020  10  15  18  18  18  18  18  18  18  18  18   0
2021  21  45  78  75  78  78  78  78  78  78  78  20

>>> df_month
      January  February  March  April  may  June  July  August  September  October  November  December
2016       20        20     20     20   20    20    20      20         20       20        20        20
2017        0        13     20     20   20    20    20      20         20       20        20        20
2018        0        10     10     10   10    10    10      10         10       10        10         0
2019        0        18     15     15   15    15    15      15         15       15        15         0
2020       10        15     18     18   18    18    18      18         18       18        18         0
2021       21        45     78     75   78    78    78      78         78       78        78        20

Full-code:

import pandas as pd
import matplotlib.pyplot as plt

years = ['2016', '2017', '2018', '2019', '2020', '2021']

dataavail1 = {'a': [20, 0, 0, 0, 10, 21], 'b': [20, 13, 10, 18, 15, 45], 'c': [20, 20, 10, 15, 18, 78], 'd': [20, 20, 10, 15, 18, 75], 'e': [20, 20, 10, 15, 18, 78], 'f': [20, 20, 10, 15, 18, 78], 'g': [20, 20, 10, 15, 18, 78], 'h': [20, 20, 10, 15, 18, 78], 'i': [20, 20, 10, 15, 18, 78], 'j': [20, 20, 10, 15, 18, 78], 'k': [20, 20, 10, 15, 18, 78], 'l': [20, 20, 0, 0, 0, 20]}

dataavail2 = {'January': [20, 0, 0, 0, 10, 21], 'February': [20, 13, 10, 18, 15, 45], 'March': [20, 20, 10, 15, 18, 78], 'April': [20, 20, 10, 15, 18, 75], 'may': [20, 20, 10, 15, 18, 78], 'June': [20, 20, 10, 15, 18, 78], 'July': [20, 20, 10, 15, 18, 78], 'August': [20, 20, 10, 15, 18, 78], 'September': [20, 20, 10, 15, 18, 78], 'October': [20, 20, 10, 15, 18, 78], 'November': [20, 20, 10, 15, 18, 78], 'December': [20, 20, 0, 0, 0, 20]}

df_alpha = pd.DataFrame(dataavail1, index=years)
df_month = pd.DataFrame(dataavail2, index=years)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 8))
df_alpha.plot(kind='bar', stacked=True, colormap=plt.cm.tab20, ax=ax1, rot=0)
df_month.plot(kind='bar', stacked=True, colormap=plt.cm.tab20, ax=ax2, rot=0)
plt.show()

enter image description here

Update: the code also works with Python 3.7.12

  • Related