【问题标题】:Trouble Plotting a Ridge Plot in Seaborn在 Seaborn 中绘制山脊图时遇到问题
【发布时间】:2019-07-28 22:34:36
【问题描述】:

我有一个数据框 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 轴上的小时,但没有成功。

【问题讨论】:

    标签: python pandas seaborn ridgeline-plot


    【解决方案1】:

    对我来说,将g 更改为Locationx 更改为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)
    

    【讨论】:

      猜你喜欢
      • 2020-05-31
      • 2019-10-20
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 2021-12-17
      相关资源
      最近更新 更多