试试下面的代码。通过为每种
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, heightgraph = 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


![在熊猫多索引数据帧上绘制两个级别的x_ticklabel [重复] 在熊猫多索引数据帧上绘制两个级别的x_ticklabel [重复]](http://www.mshxw.com/aiimages/31/662544.png)
