【问题标题】:Adjusting varying thickness of bar in seaborn barplots for subplots为子图调整 seaborn 条形图中不同的条形厚度
【发布时间】:2018-03-03 09:25:04
【问题描述】:

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

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

如何获得两个条形图的不同长度条?

%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

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    通过使用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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-08
      • 2020-02-10
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 2016-05-15
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多