在seaborn barplots中调整不同厚度的条形图以用于子图

时间:2018-03-03 09:25:05

标签: python pandas matplotlib seaborn

在一个seaborn水平条形图中,它有两组条形图,其中一组放在另一组上,每个条形图的轴是如何独立控制的?目前,我想根据其中某些实体的出现频率调整条形的厚度。

目前,两个条形图都是用ax1ax2中存储的轴绘制的。但是我只能为ax1(浅蓝色)调整条形的厚度,而不是ax2(深蓝色。所有条形均匀厚度)。我无法弄清楚如何完成ax2的分配以便调整第二组条的条形厚度。

如何获得两个条形图的变长条? Sample Plot

%matplotlib inline
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")


f, ax = plt.subplots(figsize=(15, 45))
crashes = plotie.groupby('target_wcount').mean()

sns.set_color_codes("pastel")
ax1 = sns.barplot(x="uno", y="indie", orient='h',  data=crashes,
            label="uno", color="b")
sns.set_color_codes("muted")
ax2 = sns.barplot(x="miss", y="indie", orient='h',  data=crashes,
            label="miss", color="b")


for bar, newwidth in zip(ax1.patches, summa):
    bar.set_height(3*newwidth)

for bar, newwidth in zip(ax2.patches, summa):
    bar.set_height(3*newwidth)

sns.despine(left=True, bottom=True)
f.savefig('filea')

数据样本

    output_wcount   missing_count   match_count     uni     indie   uno     miss
target_wcount                           
49  49.0    39.440000   9.560000    1.0     49  1.0     0.804898
48  48.0    36.730000   11.270000   1.0     48  1.0     0.765208
46  46.0    34.400000   11.600000   1.0     46  1.0     0.747826
45  45.0    33.940000   11.060000   1.0     45  1.0     0.754222
44  44.0    34.630000   9.370000    1.0     44  1.0     0.787045
43  43.0    31.420000   11.580000   1.0     43  1.0     0.730698
42  42.0    31.455000   10.545000   1.0     42  1.0     0.748929
41  41.0    29.630000   11.370000   1.0     41  1.0     0.722683
40  40.0    28.430000   11.570000   1.0     40  1.0     0.710750
39  39.0    27.935556   11.064444   1.0     39  1.0     0.716296

1 个答案:

答案 0 :(得分:0)

通过使用twinx功能,可以轻松解决

%matplotlib inline
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")

f, ax = plt.subplots(figsize=(15, 45))
crashes = plotie.groupby('target_wcount').mean() 
sns.set_color_codes("pastel")
ax1 = sns.barplot(x="uno", y="indie", orient='h',  data=crashes,
            label="uno", color="b")
sns.set_color_codes("muted")


ax2 = ax.twinx()


sns.barplot(x="miss", y="indie", orient='h',  data=crashes,
            label="miss", color="b",ax=ax2)


for bar, newwidth in zip(ax1.patches, summa):
    bar.set_height(3*newwidth)

for bar, newwidth in zip(ax2.patches, summa):
    bar.set_height(3*newwidth)

sns.despine(left=True, bottom=True)
f.savefig('filea')