Seaborn - 具有色调的KDE子图

时间:2017-08-09 15:56:45

标签: python matplotlib seaborn

正如标题所提到的,我正在尝试创建4个matplotlib子图,并且每个我想绘制一个由我的数据帧中的列设置的KDE图。我找到了一种“有点”的方法,这就是代码:

ScoreDiffMinus1 = ScoreMovements.loc[ScoreMovements['ScoreDifference'] == -1]
ScoreDiffMinus2 = ScoreMovements.loc[ScoreMovements['ScoreDifference'] == -2]
ScoreDiffMinus3 = ScoreMovements.loc[ScoreMovements['ScoreDifference'] == -3]
ScoreDiffMinus4 = ScoreMovements.loc[ScoreMovements['ScoreDifference'] == -4]

fg, ax = plt.subplots(2,2, figsize = (20,12.5), sharex=True, sharey=True)

Face1 = sns.FacetGrid(ScoreDiffMinus1, hue="Product")
G1 = Face1.map(sns.kdeplot, 'DateDifference', ax = ax[0,0])

Face2 = sns.FacetGrid(ScoreDiffMinus2, hue="Product")
G2 = Face2.map(sns.kdeplot, 'DateDifference', ax = ax[0,1])

Face3 = sns.FacetGrid(ScoreDiffMinus3, hue="Product")
G3 = Face3.map(sns.kdeplot, 'DateDifference', ax = ax[1,0])

Face4 = sns.FacetGrid(ScoreDiffMinus4, hue="Product")
G4 = Face4.map(sns.kdeplot, 'DateDifference', ax = ax[1,1])

这种方法的问题是我运行FacetGrid和Matplotlib子图,这意味着我有4个图表,下面我有4个来自FacetGrid的空图表。这也意味着我不能设置xlabels,title等,就像我运行一个简单的KDE图一样。

有更好的方法吗?我觉得自己缺少一些明显的东西,但上面的方法并不适合我。

由于

1 个答案:

答案 0 :(得分:0)

以下

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns

p = np.random.choice(["Fan", "Table", "Ball"], size=100)
sd = np.random.randint(-4,0,size=100)
dd = np.random.randint(0,40,size=100)
df = pd.DataFrame({'ScoreDifference': sd ,
                   'DateDifference':  dd ,
                   "Product": p})



grid = sns.FacetGrid(df, col='ScoreDifference', hue="Product", col_order=range(-4,0)[::-1])
grid.map(sns.kdeplot, 'DateDifference')
grid.add_legend()

plt.show()

enter image description here