learnerX
learnerX

Reputation: 1082

How to produce a stacked bar plot in Python3?

I have an R plot that I am trying to reproduce in python3. My R plot generates the following graph:

enter image description here

I have the following python code that isn't working correctly:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
import pandas as pd
plt.style.use("ggplot")

# Values of each group

#"2006   2014   2016   2017   2018   2019
#A 1     0         0     0       0       0
#B 0     0         0     1       0       0
#C 0     0         2     0       1       0
#D 0     0         1     2       0       0
#E 0     0         1     0       0       0
#F 0     0         0     0       0       1
#G 0     0         0     0       1       0
#H 0     1         0     0       0       0


bars1 = [1    ,0,         0,     0,       0 ,      0]
bars2 = [0   ,  0 ,        0 ,    1 ,      0 ,      0]
bars3 = [0   ,  0   ,      2  ,   0  ,     1  ,     0]
bars4 = [0   ,  0       ,  1   ,  2   ,    0  ,     0]
bars5 = [0   ,  0   ,      1   ,  0 ,      0  ,     0]
bars6 = [0   ,  0     ,    0 ,    0 ,      0  ,     1]
bars7 = [0   ,  0      ,   0  ,   0   ,    1    ,   0]
bars8 = [0  ,  1     ,    0 ,    0  ,     0   ,    0]


# Heights of bars1 + bars2
bars = np.add(bars1, bars2).tolist()

# The position of the bars on the x-axis
r = [0,1,2,3,4,5]

# Names of group and bar width
names = ['2006','2014','2016','2017','2018', '2019']
barWidth = 0.75

# Create brown bars
plt.bar(r, bars1, color='#2F3114', edgecolor='white', width=barWidth)
# Create green bars (middle), on top of the firs ones
plt.bar(r, bars2, bottom=bars1, color='#3B4E14', edgecolor='white', width=barWidth)
# Create green bars (top)
plt.bar(r, bars3, bottom=bars4, color='#A0C11B', edgecolor='white', width=barWidth)
plt.bar(r, bars4, bottom=bars3, color='#E8E7BC', edgecolor='white', width=barWidth)
plt.bar(r, bars5, bottom=bars, color='#2F1E14', edgecolor='white', width=barWidth)
plt.bar(r, bars6, bottom=bars, color='#DF2A81', edgecolor='white', width=barWidth)
plt.bar(r, bars7, bottom=bars, color='#27B916', edgecolor='white', width=barWidth)
plt.bar(r, bars8, bottom=bars, color='#A979DB', edgecolor='white', width=barWidth)

# Custom X axis
plt.xticks(r, names)
plt.xlabel("axisA")
plt.ylabel("axisB")

# Show graphic
plt.show()

How can I generate the correct stacked bar plot in python3 similar to the example figure shown above (the colors don't have to match)?

Note: the code above plot something close to what's expected but is not expected. The number of instances is off if you look closely. For example, 2017 has 3 instances (only shows 2) and 2016 has 4 (only shows 3). How to plot the embedded correctly?

Addendum: data in R dataframe:

dat <- read.table(text ="2006   2014   2016   2017   2018   2019
A 1     0         0     0       0       0
B 0     0         0     1       0       0
C 0     0         2     0       1       0
D 0     0         1     2       0       0
E 0     0         1     0       0       0
F 0     0         0     0       0       1
G 0     0         0     0       1       0
H 0     1         0     0       0       0

", header = TRUE)

Upvotes: 1

Views: 60

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150785

I saw you imported pandas, you may want to consider pandas plot function:

df = pd.DataFrame({'2006': {'A': 1, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'G': 0, 'H': 0},
 '2014': {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'G': 0, 'H': 1},
 '2016': {'A': 0, 'B': 0, 'C': 2, 'D': 1, 'E': 1, 'F': 0, 'G': 0, 'H': 0},
 '2017': {'A': 0, 'B': 1, 'C': 0, 'D': 2, 'E': 0, 'F': 0, 'G': 0, 'H': 0},
 '2018': {'A': 0, 'B': 0, 'C': 1, 'D': 0, 'E': 0, 'F': 0, 'G': 1, 'H': 0},
 '2019': {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 1, 'G': 0, 'H': 0}}
)

df.T.plot.bar(stacked=True)

and you get:

enter image description here

Upvotes: 1

Related Questions