创建 "combo" 个图的子图(hist + boxplot)

Creating a subplot of "combo" plots (hist + boxplot)

我有这个代码:

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 x 1 子图,使其看起来如下所示:

desired result

我以前使用过子图,但在这里我很难让它工作,因为我在每次迭代中同时处理两条曲线,但没能成功。外层 for 循环中的 num 用于填充子图代码,使得 ax = fig.add_subplot(1,2,num) --> 这在这里不起作用。

我相信您可以使用 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")