【问题标题】:How to produce a stacked bar plot in Python3?如何在 Python3 中生成堆积条形图?
【发布时间】:2020-08-20 21:24:36
【问题描述】:

我有一个R 的情节,我试图在python3 中重现它。我的R 绘图生成以下图表:

我的以下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()

如何在python3 中生成正确的堆积条形图,类似于上面显示的示例图(颜色不必匹配)?

注意:上面的代码绘制的东西接近预期但不是预期的。如果您仔细观察,实例的数量会关闭。例如,2017 年有 3 个实例(仅显示 2 个),2016 年有 4 个(仅显示 3 个)。如何正确绘制嵌入?

附录:R 数据帧中的数据:

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)

【问题讨论】:

  • 我看到你导入pandas。你有熊猫数据框中的数据吗?
  • 是的,我愿意。编辑了我上面的问题。编辑:实际上,该数据并不一致。一会儿。
  • 我错过了什么吗?您的 df 看起来不像上面的数据 (bars)
  • 对不起。那确实是不正确的。我没有 pandas 中的数据,但我添加了等效的 R 数据框。
  • 是的,我就是这么想的。请参阅下面的答案。

标签: python r python-3.x matplotlib ggplot2


【解决方案1】:

我看到你导入了 pandas,你可能要考虑 pandas 绘图功能:

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)

你会得到:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 2019-03-20
    • 1970-01-01
    • 2017-10-14
    • 2020-11-10
    • 2014-02-09
    相关资源
    最近更新 更多