创建"combo"图的子图(历史图+箱线图)



我有这个代码:

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)

它绘制了以下内容:

截至目前的绘图

但我想要的是创建一个简单的2乘1的子画面,使其看起来如下:

期望结果

我以前使用过子图,但现在我很难让它发挥作用,因为我在每次迭代中同时使用两条曲线,但没能让它发挥效果。外部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")

最新更新