简体   繁体   English

使用 matplotlib 的分组百分比堆叠条形图

[英]Grouped percent stacked bar plot using matplotlib

I am trying to create a "grouped percent stacked bar plot" for want of a better name.我正在尝试创建一个“分组百分比堆积条形图”,因为想要一个更好的名字。 Here is a simple working example of my code:这是我的代码的一个简单工作示例:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
import pandas as pd

# Data
r = [0,1,2,3,4]
raw_data = {'PFHxA': [20, 1.5, 7, 10, 5], \
        'PFHpA': [5, 15, 5, 10, 15], \
        'PFOA': [2, 15, 18, 5, 10], \
        'PFNA': [2, 15, 18, 5, 10], \
        'PFDA': [2, 15, 18, 5, 10], \
        'PFUnDA': [2, 15, 18, 5, 10], \
        'PFHxS': [2, 15, 18, 5, 10], \
        'PFOS': [2, 15, 18, 5, 10]}
df = pd.DataFrame(raw_data)

# From raw value to percentage
totals = [i+j+k+l+m+n+o+p for i,j,k,l,m,n,o,p in zip(df['PFHxA'], 
df['PFHpA'], df['PFOA'], df['PFNA'], df['PFDA'], df['PFUnDA'], df['PFHxS'], 
df['PFOS'])]
PFHxA = [i / j * 100 for i,j in zip(df['PFHxA'], totals)]
PFHpA = [i / j * 100 for i,j in zip(df['PFHpA'], totals)]
PFOA = [i / j * 100 for i,j in zip(df['PFOA'], totals)]
PFNA = [i / j * 100 for i,j in zip(df['PFNA'], totals)]
PFDA = [i / j * 100 for i,j in zip(df['PFDA'], totals)]
PFUnDA = [i / j * 100 for i,j in zip(df['PFUnDA'], totals)]
PFHxS = [i / j * 100 for i,j in zip(df['PFHxS'], totals)]
PFOS = [i / j * 100 for i,j in zip(df['PFOS'], totals)]

# plot
barWidth = 0.85
names = ('A','B','C','D','E')
# Create PFHxA Bars
plt.bar(r, PFHxA, color='#A20101', edgecolor='white', width=barWidth, 
label="PFHxA")
# Create PFHpA Bars
plt.bar(r, PFHpA, bottom=PFHxA, color='#F44E54', edgecolor='white', 
width=barWidth, label="PFHpA")
# Create PFOA Bars
plt.bar(r, PFOA, bottom=[i+j for i,j in zip(PFHxA, PFHpA)], color='#FF9904', 
edgecolor='white', width=barWidth, label="PFOA")
# Create PFNA Bars
plt.bar(r, PFNA, bottom=[i+j+k for i,j,k in zip(PFHxA, PFHpA, PFOA)], 
color='#FDDB5E', edgecolor='white', width=barWidth, label="PFNA")
# Create PFDA Bars
plt.bar(r, PFDA, bottom=[i+j+k+l for i,j,k,l in zip(PFHxA, PFHpA, PFOA, 
PFNA)], color='#BAF1A1', edgecolor='white', width=barWidth, label="PFDA")
# Create PFUnDA Bars
plt.bar(r, PFUnDA, bottom=[i+j+k+l+m for i,j,k,l,m in zip(PFHxA, PFHpA, 
PFOA, PFNA, PFDA)], color='#76AD3B', edgecolor='white', width=barWidth, 
label="PFUnDA")
# Create PFHxS Bars
plt.bar(r, PFHxS, bottom=[i+j+k+l+m+n for i,j,k,l,m,n in zip(PFHxA, PFHpA, 
PFOA, PFNA, PFDA, PFUnDA)], color='#0A709A', edgecolor='white', 
width=barWidth, label="PFHxS")
# Create PFOS Bars
plt.bar(r, PFOS, bottom=[i+j+k+l+m+n+o for i,j,k,l,m,n,o in zip(PFHxA, 
PFHpA, PFOA, PFNA, PFDA, PFUnDA, PFHxS)], color='#5E3F99', 
edgecolor='white', width=barWidth, label="PFOS")


# Custom x axis
plt.xticks(r, names)
plt.xlabel("Sample ID")

# Custom y axis
plt.ylabel("Mass percentage")

# Add a legend
plt.legend(loc='upper left', bbox_to_anchor=(1,1), ncol=1)

# Show graphic
plt.show()

This works fine and I create the following percent stacked barplot:这很好用,我创建了以下百分比堆叠条形图: 百分比堆叠条形图 . .

However, I would like to add some extra white space between samples B and C and between samples D and E since I want to highlight that there are three sample groups (A and B are the first group, C and D are the second group and E is the third group).但是,我想在样本 B 和 C 之间以及样本 D 和 E 之间添加一些额外的空白,因为我想强调存在三个样本组(A 和 B 是第一组,C 和 D 是第二组, E是第三组)。 Is there a simple way to do this (such that I can expand it to more than three groups)?有没有一种简单的方法可以做到这一点(这样我就可以将其扩展到三个以上的组)? Ideally, I would also like to be able to add a text label above each group - is this also possible?理想情况下,我还希望能够在每个组上方添加一个文本标签——这也可能吗?

The code seems much too complicated for asking about the spacings between bars, since this is determined by r you can change r to eg代码似乎太复杂了,无法询问条之间的间距,因为这是由r决定的,您可以将r更改为 eg

r = [0,1,2.5,3.5,5]

A text can be added via plt.text可以通过plt.text添加文本

for pos, text in r, texts:
    plt.text(r, 101, text)

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

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