使用matplotlib分组百分比堆积条形图 [英] Grouped percent stacked bar plot using matplotlib

查看:749
本文介绍了使用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: .

但是,我想在样本B和C之间以及样本D和E之间添加一些额外的空白,因为我想强调三个样本组(A和B是第一个样本组,C和D是第一个样本组).第二组,E是第三组).有没有简单的方法可以做到这一点(以便我可以将其扩展到三个以上的组)?理想情况下,我还希望能够在每个组的上方添加一个文本标签-也可以吗?

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). 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?

推荐答案

对于询问条形之间的间距,代码似乎太复杂了,因为这是由r确定的,因此您可以将r更改为eg

The code seems much too complicated for asking about the spacings between bars, since this is determined by r you can change r to e.g.

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

可以通过plt.text

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

这篇关于使用matplotlib分组百分比堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆