我有一个多索引数据框,其中的索引已从日期导出。它包含年份和季度值。
我要实现的是在x轴上具有两组刻度线标签的图。次刻度标签应代表四分之一值(1到4),而大刻度标签应代表年值。但是,我不希望显示所有年份的刻度标签,而只显示四个季度中每个季度的唯一年份。
这是我的数据集中的一个样本。
serotype_df = pd.DataFrame({'13v': {(2002, 1): 5,
(2002, 2): 9,
(2002, 3): 23,
(2002, 4): 11,
(2003, 1): 1,
(2003, 2): 12,
(2003, 3): 22,
(2003, 4): 15,
(2004, 1): 10,
(2004, 2): 11,
(2004, 3): 30,
(2004, 4): 11,
(2005, 1): 9,
(2005, 2): 20,
(2005, 3): 20,
(2005, 4): 7},
'23v': {(2002, 1): 1,
(2002, 2): 8,
(2002, 3): 18,
(2002, 4): 5,
(2003, 1): 5,
(2003, 2): 16,
(2003, 3): 13,
(2003, 4): 7,
(2004, 1): 4,
(2004, 2): 4,
(2004, 3): 20,
(2004, 4): 5,
(2005, 1): 4,
(2005, 2): 5,
(2005, 3): 10,
(2005, 4): 5},
'7v': {(2002, 1): 30,
(2002, 2): 75,
(2002, 3): 148,
(2002, 4): 68,
(2003, 1): 26,
(2003, 2): 75,
(2003, 3): 147,
(2003, 4): 67,
(2004, 1): 32,
(2004, 2): 84,
(2004, 3): 151,
(2004, 4): 62,
(2005, 1): 21,
(2005, 2): 49,
(2005, 3): 81,
(2005, 4): 26},
'Non-typed': {(2002, 1): 1,
(2002, 2): 2,
(2002, 3): 4,
(2002, 4): 4,
(2003, 1): 3,
(2003, 2): 5,
(2003, 3): 9,
(2003, 4): 8,
(2004, 1): 1,
(2004, 2): 4,
(2004, 3): 6,
(2004, 4): 4,
(2005, 1): 4,
(2005, 2): 10,
(2005, 3): 7,
(2005, 4): 11},
'Non-vaccine': {(2002, 1): 2,
(2002, 2): 7,
(2002, 3): 10,
(2002, 4): 6,
(2003, 1): 4,
(2003, 2): 5,
(2003, 3): 13,
(2003, 4): 8,
(2004, 1): 2,
(2004, 2): 4,
(2004, 3): 19,
(2004, 4): 8,
(2005, 1): 4,
(2005, 2): 3,
(2005, 3): 15,
(2005, 4): 5}})
我尝试使用其他SO示例中的一些代码。这是我尝试的代码。
import pandas as pd
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(14,8), dpi=200)
ax = fig.add_subplot(111)
ax1 = ax.twiny()
serotype_df.plot(kind='bar', ax=ax, stacked='True');
trunc = lambda x: x.strip("()").split(" ")[1]
tl = [ trunc(t.get_text()) for t in ax.get_xticklabels()]
ax.set_xticklabels(tl,rotation=0);
serotype_df.plot(kind='bar', ax=ax1, stacked='True');
trunc0 = lambda x: x.strip("()").split(", ")[0]
tl = [ trunc0(t.get_text()) for t in ax1.get_xticklabels()]
ax1.set_xticklabels(tl);
我的四分之一xlabel恰好位于我想要的位置。我似乎无法获得唯一的年份值。
非常感谢您的帮助。
答案 0 :(得分:2)
尝试以下代码。通过为您的案例level[0] index
中的每个year
创建一个子图并将其用作x_label来实现。并为每个子图绘制数据。
def plot_function(x, ax):
ax = graph[x]
ax.set_xlabel(x, weight='bold')
return serotype_df.xs(x).plot(kind='bar', stacked='True', ax=ax, legend=False)
n_subplots = len(serotype_df.index.levels[0])
fig, axes = plt.subplots(nrows=1, ncols=n_subplots, sharey=True, figsize=(14, 8)) # width, height
graph = dict(zip(serotype_df.index.levels[0], axes))
plots = list(map(lambda x: plot_function(x, graph[x]), graph))
ax.tick_params(axis='both', which='both', length=0)
fig.subplots_adjust(wspace=0)
plt.legend()
plt.show()
如果您不需要对每个子图进行太多更改,则可以始终执行以下操作:
plots = list(map(lambda x: serotype_df.xs(x).plot(kind='bar', stacked='True', ax=graph[x], legend=False).set_xlabel(x, weight='bold'), graph))
那样,您不必创建或使用plot_function