简体   繁体   中英

Trouble Plotting a Ridge Plot in Seaborn

I have a dataframe hour_dist that shows the hour a customer showed up to a particular location.

hour_dist.sample(5)

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

I'm trying to plot this data with Seaborn to visualize a ridgeline plot ( https://seaborn.pydata.org/examples/kde_ridgeplot.html ).

It should essentially show the hour distribution by each location. Here's an example of what it looks like:

在此处输入图片说明

With hour_dist, I've been trying, unsuccessfully, to plot the locations on the y axis and the hour on the x axis.

For me working change g to Location and x to Hour , but if many unique Location values there should be many plots with real data:

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)

If need plot by percentage:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM