【问题标题】:Add an additional axes to seaborn heatmap向 seaborn 热图添加额外的轴
【发布时间】: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


【解决方案1】:

seaborn 的heatmap 是一个轴级别的函数,这意味着您可以预先创建您想要的任何轴,并在调用heatmap 时传递对您希望 seaborn 使用的 Axes 对象的引用。

uniform_data = np.random.rand(10, 12)
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(6,4), gridspec_kw={'width_ratios':(5,1)})
sns.heatmap(uniform_data, ax=ax1)
ax2.plot([1,2],[3,4], 'ro-')

【讨论】:

  • 太棒了!非常感谢!这应该让我到那里!很抱歉没有提供更多数据。
猜你喜欢
  • 2019-11-19
  • 2022-12-14
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
  • 2021-08-03
  • 2017-04-16
  • 2021-04-07
相关资源
最近更新 更多