Python 如何在 Seaborn 中叠加两个图形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32899463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:26:37  来源:igfitidea点击:

How can I overlay two graphs in Seaborn?

pythonseaborn

提问by Davoud Taghawi-Nejad

How can I overlay two graphs in Seaborn? I have two columns in my data I would like to have them in the same graph. How can I do it preserving the labeling for both graphs.

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

采纳答案by Paul H

seaborn function that operate on a single Axes can take one as an argument.

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

For instance, the docs to seaborn.kdeplotinclude:

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

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

So if you did:

所以如果你这样做:

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

You could then do:

然后你可以这样做:

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

回答by user2707389

The simplest example would be:

最简单的例子是:

import matplotlib.pyplot as plt

data1 = [1, 2, 3, 4, 5]

data2 = [1, 1.1, 1.3, 4, 4.1]

def plotter():
    plt.plot(data1)
    plt.plot(data2)
    plt.show()


plotter()

回答by Davoud Taghawi-Nejad

One solution is to introduce a secordary axis:

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

    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

在此处输入图片说明

回答by Omar Villa

The data is about Private vs Public collage data but works, as we can see we load all the global parameters to a seaborn object and later we map the charts to the same pane.

数据是关于私人与公共拼贴数据但有效,因为我们可以看到我们将所有全局参数加载到一个 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

见图表