简体   繁体   English

在 Python 中使用 seaborn 一起绘制 distplot 和 boxplot 时修复倒置的 distplot

[英]Fix inverted distplot when plotting distplot and boxplot together using seaborn in Python

My DataFrame is as below:-我的 DataFrame 如下:-

spending    advance_payments    probability_of_full_payment     current_balance     credit_limit    min_payment_amt     max_spent_in_single_shopping
0   19.94   16.92   0.8752  6.675   3.763   3.252   6.550
1   15.99   14.89   0.9064  5.363   3.582   3.336   5.144
2   18.95   16.42   0.8829  6.248   3.755   3.368   6.148
3   10.83   12.96   0.8099  5.278   2.641   5.182   5.185
4   17.99   15.86   0.8992  5.890   3.694   2.068   5.837
...     ...     ...     ...     ...     ...     ...     ...
205     13.89   14.02   0.8880  5.439   3.199   3.986   4.738
206     16.77   15.62   0.8638  5.927   3.438   4.920   5.795
207     14.03   14.16   0.8796  5.438   3.201   1.717   5.001
208     16.12   15.00   0.9000  5.709   3.485   2.270   5.443
209     15.57   15.15   0.8527  5.920   3.231   2.640   5.879

Now I am trying to plot a boxplot on top of the displot using seaborn libraries.现在我正在尝试使用 seaborn 库在显示的顶部 plot 箱线图。 Distplot on its own works fine, however when I add boxplot on the same axis, the distplot gets inverted, not sure why that is happening. Distplot 本身工作正常,但是当我在同一轴上添加 boxplot 时,distplot 会倒置,不知道为什么会这样。 Can someone please help me fix this?有人可以帮我解决这个问题吗?

Below is my code:-以下是我的代码: -

fig, axs = plt.subplots(3, 3,figsize=(10,10))
j=0
k=0
for i in df.columns:
    sns.distplot(df[i],ax=axs[j,k],color='blue')
    sns.boxplot(df[i],ax=axs[j,k],color='green',boxprops=dict(alpha=.5))
    plt.gca().invert_xaxis()
    k=k+1
    if k==3:
        k=0
        j=j+1
fig.tight_layout()
plt.show()

I have tried plt.gca().invert_xaxis() but that did not help.我试过plt.gca().invert_xaxis()但这没有帮助。

Below is the output I am getting.下面是我得到的 output。

在此处输入图像描述

Adding different charts type on same figure is what causing you the problem, as seaborn does not handle them well.在同一图形上添加不同的图表类型是导致问题的原因,因为 seaborn 不能很好地处理它们。 Instead, seaborn offer built-in plot options to combine different charts.相反,seaborn 提供内置 plot 选项来组合不同的图表。 In your case, you want to combine box plot with kernel estimate.在您的情况下,您希望将框 plot 与 kernel 估计结合起来。 Seaborn allows you to do that with violinplot . Seaborn 允许您使用violinplot做到这一点。

It can yields something like that:它可以产生类似的东西: 在此处输入图像描述

(taken from documentation that a linked above) (取自上面链接的文档)

The format looks a bit different from what you tried to do, but essentially it shows same data: frequency with boxplot.格式看起来与您尝试做的有点不同,但本质上它显示相同的数据:带有箱线图的频率。

Code for your case:您的案例代码:

fig, axs = plt.subplots(3, 3,figsize=(10,10))
j=0
k=0
for i in df.columns:
    sns.violinplot(df[i],ax=axs[j,k],cut=0)
    k=k+1
    if k==3:
        k=0
        j=j+1
fig.tight_layout()
plt.show()

I'm keep searching for a good reason why distplot is inverted when plotting together with boxplot.我一直在寻找一个很好的理由,为什么在与箱线图一起绘制时 distplot 是倒置的。 So far I couldn't find a good answer.到目前为止,我找不到一个好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM