我如何在 Seaborn 中叠加两个图表?

How can I overlay two graphs in Seaborn?

如何在 Seaborn 中叠加两个图表?我的数据中有两列,我希望将它们放在同一张图中。我怎样才能保留两个图的标签。

在单个 Axes 上运行的 seaborn 函数可以将一个作为参数。

例如,seaborn.kdeplot 的文档包括:

ax : matplotlib axis, optional
    Axis to plot on, otherwise uses current axis

如果你这样做了:

df = function_to_load_my_data()
fig, ax = plt.subplots()

然后你可以这样做:

seaborn.kdeplot(df['col1'], ax=ax)
seaborn.kdeplot(df['col2'], ax=ax)

一种解决方案是引入辅助轴:

    fig, ax = plt.subplots()
    sb.regplot(x='round', y='money', data=firm, ax=ax)
    ax2 = ax.twinx()
    sb.regplot(x='round', y='dead', data=firm, ax=ax2, color='r')
    sb.plt.show()

数据是关于 Private 与 Public 拼贴数据,但有效,正如我们所见,我们将所有全局参数加载到 seaborn 对象,稍后我们将图表映射到同一窗格。

import seaborn as sns

import matplotlib.pyplot as plt

import pandas as pd


df = pd.read_csv('College_Data',index_col=0)

g = sns.FacetGrid(df,hue='Private',palette='coolwarm',size=6,aspect=2)

g.map(plt.hist,'Outstate',bins=20,alpha=0.7)

See Chart