【发布时间】:2018-05-14 16:39:45
【问题描述】:
我有一个留存矩阵作为 seaborn 热图。我想知道,我可以在哪里以及如何添加一个额外的轴。我想在热图旁边有一个额外的垂直条形图,以显示给定群组中的新用户数量。任何帮助或指示将不胜感激。
我尝试使用 fig.add_axes,但对于使用哪些参数和值有些迷茫。 干杯
parameter = 'Total Rent'
recordtpye = 'Tenant'
dataset = df[(df['Account Record Type'] == recordtpye)]
grouped = dataset.groupby(['TenanatCohort','BookingPeriod'])
cohorts = grouped.agg({'Request ID': pd.Series.nunique,'Total Rent':np.sum})
cohorts.rename(columns={'Request ID': 'Number of Bookings'}, inplace=True)
cohorts.reset_index(inplace=True)
cohorts.set_index(['TenanatCohort', 'BookingPeriod'], inplace=True)
cohort_group_size = dataset.groupby('TenanatCohort').agg({'Tenant: Account Name': pd.Series.nunique})
user_retention_numbers = cohorts[parameter].unstack(0)
bookings_per_cohort = \
cohorts.reset_index()\
.groupby('TenanatCohort')\
.agg({parameter: np.sum})[parameter]\
.divide(cohort_group_size['Tenant: Account Name'])
inp = user_retention_numbers
sns.set(style='white')
fig = plt.figure(figsize=(16,7))
graph = sns.heatmap(inp.T,
mask=inp.T.isnull(),
cmap="Blues",
annot=True,
fmt=".0f",
annot_kws={"size":10});
graph.set_xlabel("Booking Period")
graph.set_ylabel("Cohort Group")
plt.yticks(rotation=0)
【问题讨论】:
-
像往常一样,如果您提供Minimal, Complete, and Verifiable example(即不依赖于您的特定数据集)将很有用。此外,请提供所需输出的模型。你想绘制什么样的“垂直图表”
标签: python pandas matplotlib seaborn