matplotlib中的“panel barchart”

时间:2014-11-19 05:28:02

标签: matplotlib pandas bar-chart facet

我想使用matplotlib生成一个像这样的图:

"panel barchart" http://peltiertech.com/images/2010-03/tabgart6_colpanel_labels_lg.png

我的数据是在一个pandas DataFrame中,我已经到了常规的堆叠条形图,但我无法弄清楚如何为每个类别赋予其自己的y轴基线。

理想情况下,我希望所有子图的垂直比例完全相同,并将面板标签移到侧面,以便行之间没有间隙。

1 个答案:

答案 0 :(得分:3)

我没有完全复制你想要的东西,但这应该让你非常接近。

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

#create dummy data
cols = ['col'+str(i) for i in range(10)]
ind = ['ind'+str(i) for i in range(10)]
df = pd.DataFrame(np.random.normal(loc=10, scale=5, size=(10, 10)), index=ind, columns=cols)

#create plot
sns.set_style("whitegrid")
axs = df.plot(kind='bar', subplots=True, sharey=True, 
              figsize=(6, 5), legend=False, yticks=[], 
              grid=False, ylim=(0, 14), edgecolor='none', 
              fontsize=14, color=[sns.xkcd_rgb["brownish red"]])
plt.text(-1, 100, "The y-axis label", fontsize=14, rotation=90)  # add a y-label with custom positioning
sns.despine(left=True)  # get rid of the axes
for ax in axs:  # set the names beside the axes
    ax.lines[0].set_visible(False)  # remove ugly dashed line
    ax.set_title('')
    sername = ax.get_legend_handles_labels()[1][0]
    ax.text(9.8, 5, sername, fontsize=14)
plt.suptitle("My panel chart", fontsize=18)

Example chart with dummy data