【问题标题】:Creating a subplot of "combo" plots (hist + boxplot)创建“组合”图的子图(hist + boxplot)
【发布时间】:2020-03-19 22:59:32
【问题描述】:

我有这个代码:

def data_analysis(column_name):
    analysis = ['ultimate', 'surf']
    for i, num in zip(analysis, range(1,len(analysis)+1)):

        df = df_summary[(df_summary['tariff'] ==  i)]
        sns.set(style="ticks")
        x = df[column_name]
        f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, 
                                            gridspec_kw={"height_ratios": (.2, .85)})
        sns.boxplot(x, ax=ax_box)
        sns.distplot(x, ax=ax_hist, bins = 50)
        ax_box.set(yticks=[])
        table = '\nDescriptive Stats: \n', df[column_name].describe()
        ax_box.text(df[column_name].mean()*0.2,-1,table,size=12)
        sns.despine(ax=ax_hist)
        sns.despine(ax=ax_box, left=True)

绘制以下内容:

plots as of now

但我正在寻找的是创建一个简单的 2 乘 1 子图,使其如下所示:

desired result

我以前使用过子图,但在这里我正在努力使其工作,因为我在每次迭代中同时使用两条曲线并且没有设法使其工作。外部 for 循环中的 num 用于填充子图代码,以便 ax = fig.add_subplot(1,2,num) --> 这在这里不起作用。

【问题讨论】:

    标签: python-3.x matplotlib subplot


    【解决方案1】:

    我相信你可以使用GridSpec 完成你想要的。以下代码应使用模拟数据生成您想要的:

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import matplotlib.gridspec as gridspec
    
    # Fake data
    np.random.seed(20200319)
    left_data = np.random.randint(low = 0, high = 1000, size = (161, 1))
    right_data = np.random.randint(low = 0, high = 1000, size = (339, 1))
    left_data = np.random.randn(161, 1) * 191.3 + 367.3
    right_data = np.random.randn(339, 1) * 189.1 + 369.5
    
    # Create a figure and two main subplots
    f = plt.figure()
    gs = gridspec.GridSpec(1, 2, figure=f)
    
    # Create subplots for the Left subplot
    gs_left = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec = gs[0])
    ax_left_box = f.add_subplot(gs_left[0, :])
    ax_left_hist = f.add_subplot(gs_left[1, :], sharex = ax_left_box)
    
    # Create subplots for the right subplot
    gs_right = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec = gs[1])
    ax_right_box = f.add_subplot(gs_right[0, :])
    ax_right_hist = f.add_subplot(gs_right[1, :], sharex = ax_right_box)
    
    # Populate with data
    ax_left_box.boxplot(left_data, vert = False)
    ax_left_hist.hist(left_data, bins = np.arange(0,1001,20), density = True)
    ax_right_box.boxplot(right_data, vert = False)
    ax_right_hist.hist(right_data, bins = np.arange(0,1001,20), density = True)
    
    # Formatting
    ax_left_box.set_xlim((-200, 1200))
    ax_left_box.set_xticks(np.arange(-200, 1201, 200))
    ax_left_hist.set_ylim((0, 0.004))
    ax_left_hist.set_xlabel("call_duration_per_month")
    
    ax_right_box.set_xlim((-200, 1200))
    ax_right_box.set_xticks(np.arange(-200, 1201, 200))
    ax_right_hist.set_ylim((0, 0.004))
    ax_right_hist.set_xlabel("call_duration_per_month")
    
    plt.suptitle("The main title")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多