簡體   English   中英

向Seaborn熱圖添加其他軸

[英]Add an additional axes to seaborn heatmap

我有一個保留矩陣作為海底熱圖。 我想知道如何在哪里以及如何添加附加軸。 我想在熱圖旁邊附加一個垂直條形圖,以顯示給定同類群組中的新用戶數量。 任何幫助或指針將不勝感激。

我嘗試使用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)

seaborn的heatmap是一個軸水平儀功能,這意味着你可以創建你事先想要的任何軸,並傳遞給軸的參考對象,你想seaborn打電話時使用heatmap

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-')

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM