簡體   English   中英

在 Seaborn 中繪制嶺圖時遇到問題

[英]Trouble Plotting a Ridge Plot in Seaborn

我有一個數據框 hour_dist 顯示客戶出現在特定位置的小時數。

hour_dist.sample(5)

        Location            Hour
88131   1233000000000000    21
111274  1233000000000000    0
81126   2991000000000000    23
104181  1232000000000000    22
55719   1232000000000000    15

我正在嘗試使用 Seaborn 繪制此數據以可視化山脊線圖 ( https://seaborn.pydata.org/examples/kde_ridgeplot.html )。

它應該基本上顯示每個位置的小時分布。 下面是它的外觀示例:

在此處輸入圖片說明

使用hour_dist,我一直在嘗試繪制y 軸上的位置和x 軸上的小時,但沒有成功。

對我來說,將g更改為Location並將x更改為Hour ,但是如果有許多唯一的Location值,那么應該有很多帶有真實數據的圖:

import numpy as np
import pandas as pd
import seaborn as sns

import matplotlib.pyplot as plt
sns.set(style="white", rc={"axes.facecolor": (0, 0, 0, 0)})


# Initialize the FacetGrid object
pal = sns.cubehelix_palette(10, rot=-.25, light=.7)
g = sns.FacetGrid(df, row="Location", hue="Location", aspect=15, height=.5, palette=pal)

如果需要按百分比繪制:

#df['pct'] = df['Location'].div(df.groupby('Hour')['Location'].transform('sum'))
#g = sns.FacetGrid(df, row="pct", hue="pct", aspect=15, height=.5, palette=pal)


# Draw the densities in a few steps
g.map(sns.kdeplot, "Hour", clip_on=False, shade=True, alpha=1, lw=1.5, bw=.2)
g.map(sns.kdeplot, "Hour", clip_on=False, color="w", lw=2, bw=.2)
g.map(plt.axhline, y=0, lw=2, clip_on=False)


# Define and use a simple function to label the plot in axes coordinates
def label(x, color, label):
    ax = plt.gca()
    ax.text(0, .2, label, fontweight="bold", color=color,
            ha="left", va="center", transform=ax.transAxes)


g.map(label, "Hour")

# Set the subplots to overlap
g.fig.subplots_adjust(hspace=-.25)

# Remove axes details that don't play well with overlap
g.set_titles("")
g.set(yticks=[])
g.despine(bottom=True, left=True)

暫無
暫無

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

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