首页 > 解决方案 > 如果通过子图 matplotlib 绘制 2 个图,boxplot 不显示

问题描述

我正在尝试通过在 Python 中使用 matplotlib 并排绘制散点图和箱线图。

预期的结果应该是左侧的散点图,而右侧的箱线图。但是我的箱线图显示不正确。我得到如下输出:

在此处输入图像描述 如图所示,箱线图被错误地显示为丑陋的散点图。请在下面找到我的代码。我将不胜感激任何想法或建议。谢谢你的时间。

def multiple_plot(X, y, outliers_low, outliers_high,attribute_name):
    import matplotlib.pyplot as plt
    import pylab
    import seaborn as sns
    #plt.scatter(X,y)
    fig,(ax1, ax2) = plt.subplots(ncols=2,sharey=False,sharex=False,figsize=(18,5))
    ax1.set_ylim([outliers_low,outliers_high])
    sns.regplot(x=X,y=y,ax=ax1)
    ax1.set(xlabel=attribute_name)

    sns.boxplot(x=X, y=y,width=0.5, whis=2,ax=ax2)
    ax2.set(xlabel=attribute_name)
    ax2.set(ylabel='NPI');

    plt.tight_layout()
    plt.show()

标签: pythonmatplotlibplotboxplot

解决方案


推荐阅读