如何在Seaborn中覆盖两个图形?

2024-04-25 20:31:07 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
3条回答

在单轴上操作的seaborn函数可以将一个作为参数。

例如,要^{}的文档包括:

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

enter image description here

这些数据是关于私有和公共拼贴数据的,但是可以工作,正如我们看到的,我们将所有全局参数加载到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

相关问题 更多 >