Seaborn剥离图删除图

时间:2018-10-11 09:51:40

标签: python matplotlib seaborn boxplot

我有一个箱形图:

fig, ax = plt.subplots(1,1)
bp = df.boxplot(column='transaction_value', 
           by='store_type', grid=True, 
           ax=ax, showfliers=True)
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
ax.set_ylim([0, 800])
ax.set_ylabel('transaction_value')
plt.show()

enter image description here

我有一条血腥的地衣:

bplot=sns.stripplot(y='transaction_value', x='store_type', 
                   data=df, 
                   jitter=True, 
                   marker='o', 
                   alpha=0.1,
                   color='black')

enter image description here

当我尝试将带状图叠加在箱形图上时,它会删除第一个箱形图(在最左侧)。

fig, ax = plt.subplots(1,1)
bp = df.boxplot(column='transaction_value', 
           by='store_type', grid=True, 
           ax=ax, showfliers=True)
bplot=sns.stripplot(y='transaction_value', x='store_type', 
                   data=df, 
                   jitter=True, 
                   marker='o', 
                   alpha=0.1,
                   color='black')
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
ax.set_ylim([0, 500])
ax.set_ylabel('transaction_value')
plt.show()

enter image description here

如何阻止这种情况发生?

添加的数据示例:

a
    transaction_value  store_type
0           30.927648     express
1           20.356693       extra
2           48.201950       metro
3           77.213957       metro
4           15.482211  superstore
5           85.794876  superstore
6           16.199844       extra
7            0.007816  superstore
8           50.925737       metro
9           81.393811       metro
10           7.616312  superstore
11          82.172441       metro
12          49.608503       extra
13          71.907878       metro
14          85.833738  superstore
15          88.131029     express
16          11.541427       extra
17          89.759724       metro
18          96.435902  superstore
19          91.984656  superstore
20          67.795293       metro
21          39.806654  superstore
22          39.565823       metro
23          37.507718  superstore
24          37.918300       metro
25          18.599158       metro
26           3.815219       extra
27          83.210068     express
28           3.988503       extra
29          94.298953  superstore

a = pd.read_clipboard()
fig, ax = plt.subplots(1,1)
bp = a.boxplot(column='transaction_value', 
           by='store_type', grid=True, 
           ax=ax, showfliers=True)
bplot=sns.stripplot(y='transaction_value', x='store_type', 
                   data=a, 
                   jitter=True, 
                   marker='o', 
                   alpha=0.1,
                   color='black')
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
ax.set_ylim([0, 500])
ax.set_ylabel('transaction_value')
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:1)

@ImportanceOfBeingErnest在我键入内容时在注释中提供了一种解决方案,但我将提出其他建议:

为了获得更好的一致性,我建议也使用seaborn进行箱形图绘制,这应该确保两个图的布局方式相同,

fig, ax = plt.subplots(1,1)
sns.boxplot(y='transaction_value', x='store_type', data=df, ax=ax, 
            color='w')
sns.stripplot(y='transaction_value', x='store_type', data=df, ax=ax,
                   jitter=True, 
                   marker='o', 
                   alpha=0.1,
                   color='black')

ax.set_ylabel('transaction_value')
plt.show()