有时不尝试调整 seaborn 的方法会更容易,而是直接使用 matplotlib 并从头开始构建图表。
在这里,我们可以假设有一个名为 counts 的数据框,看起来像
hue c m w
class
A 20 31 29
B 40 112 63
C 85 203 117
其中索引是沿 x 轴的位置,列是不同的色调。在下文中,groupedbarplot 是将此类数据框作为输入并将条形图绘制为组的函数,并为每个条形添加标签。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(42)
def groupedbarplot(df, width=0.8, annotate="values", ax=None, **kw):
ax = ax or plt.gca()
n = len(df.columns)
w = 1./n
pos = (np.linspace(w/2., 1-w/2., n)-0.5)*width
w *= width
bars = []
for col, x in zip(df.columns, pos):
bars.append(ax.bar(np.arange(len(df))+x, df[col].values, width=w, **kw))
for val, xi in zip(df[col].values, np.arange(len(df))+x):
if annotate:
txt = val if annotate == "values" else col
ax.annotate(txt, xy=(xi, val), xytext=(0,2),
textcoords="offset points",
ha="center", va="bottom")
ax.set_xticks(np.arange(len(df)))
ax.set_xticklabels(df.index)
return bars
df = pd.DataFrame({"class" : np.random.choice(list("ABC"), size=700, p=[.1,.3,.6]),
"hue" : np.random.choice(["m", "w" ,"c"], size=700, p=[.5,.3,.2])})
counts = df.groupby(["class", "hue"]).size().unstack()
groupedbarplot(counts, annotate="col")
plt.show()
我们也可以直接标记值,groupedbarplot(counts, annotate="values")