问题
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). 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?
回答1:
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]
A text can be added via plt.text
for pos, text in r, texts:
plt.text(r, 101, text)
来源:https://stackoverflow.com/questions/49135269/grouped-percent-stacked-bar-plot-using-matplotlib