【问题标题】:Seaborn stripplot deletes plotSeaborn stripplot 删除情节
【发布时间】:2018-10-11 18:52:55
【问题描述】:

我有一个箱线图:

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()

我有一个seaborn stripplot:

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

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

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()

我怎样才能阻止这种情况发生?

添加数据示例:

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()

【问题讨论】:

  • 似乎stripplot()boxplot() 似乎无法就每个类别的放置位置达成一致。似乎stripplot() 在 [0-3] 处绘制,boxplot() 在 [1-4] 处绘制。提供Minimal, Complete, and Verifiable example 会很有帮助。提供模型数据。具体参考How to make good reproducible pandas examples
  • @DizietAsahi 添加了使用示例数据的示例。干杯
  • df.boxplot(..., positions=np.arange(4))。为了以防万一,请使用stripplot 中的order 参数来确保两个图相关。
  • @ImportanceOfBeingErnest 干杯。你想添加真实的答案还是欺骗?
  • 如果你找到了这个的副本(应该存在,但可能不是 boxplot 和 stripplot 的确切组合?)你可以这样标记。

标签: python matplotlib seaborn boxplot


【解决方案1】:

@ImportanceOfBeingErnest 在我打字时在 cmets 中提供了一个解决方案,但我打算提出其他建议:

为了更好的一致性,我建议也使用 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()

【讨论】:

  • Cheers Diziet,在这种情况下,我想将 Pandas / seaborn 分开,但这是可行的。
猜你喜欢
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
  • 2016-06-03
  • 2021-05-01
  • 1970-01-01
  • 2019-01-12
  • 2019-08-01
  • 1970-01-01
相关资源
最近更新 更多