简体   繁体   English

如何在 Python3 中生成堆叠条 plot?

[英]How to produce a stacked bar plot in Python3?

I have an R plot that I am trying to reproduce in python3 .我有一个R plot 我试图在python3中重现。 My R plot generates the following graph:我的R plot 生成以下图表:

在此处输入图像描述

I have the following python code that isn't working correctly:我有以下无法正常工作的python代码:

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)?如何在python3中生成正确的堆叠条 plot 类似于上面显示的示例图(colors 不必匹配)?

Note: the code above plot something close to what's expected but is not expected.注意:上面的代码 plot 接近预期但不是预期的。 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).例如,2017 年有 3 个实例(仅显示 2 个),2016 年有 4 个(仅显示 3 个)。 How to plot the embedded correctly?如何正确嵌入 plot?

Addendum: data in R dataframe:附录: 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)

I saw you imported pandas, you may want to consider pandas plot function:我看到你进口的pandas,你可以考虑pandas plot ZC1C425268E18385D1AB507FAC

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:你得到:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM