Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Seaborn中叠加两个图形?_Python_Seaborn - Fatal编程技术网

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

Python 如何在Seaborn中叠加两个图形?,python,seaborn,Python,Seaborn,如何在Seaborn中叠加两个图形?我的数据中有两列,我希望它们在同一个图表中。我怎样做才能保留两个图的标签呢 最简单的例子是: 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() 在单轴上运行的seabor

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

最简单的例子是:

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

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

例如,文档将包括:

因此,如果你这样做了:

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

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

谢谢,唯一的缺点是标签不适用于两个图形。@DavoudTaghawi-Nejad您有
ax
对象,因此您可以在这一点上做任何事情,真的。在这种情况下,我如何添加图例?@famargar不清楚这个问题是什么。什么样的图表?您可以使用seaborn制作多种图形,正确的方法因您所做的操作而异。变量之间的关系是什么?“保留标签”是什么意思?您希望如何区分这两个变量?在任何情况下,只要有一个普遍的答案,它将是一个关于matplotlib的答案,而不是seaborn。(1)任何类型的图形(2)当然(3)标签仍然存在。两个图形的轴标签例如(4)颜色如前所述,这取决于图形的类型。在这个相关的问题中,我给出了一个覆盖
regplot
s的例子,通过设置
FacetGrid
并通过
map()
ing添加图层。本例使用的是本机
matplotlib
,而不是seaborn,对吗?在这种情况下,如果有一个图例可能会有所帮助。为此,可以在seaborn函数中使用label参数,但显然需要在每个绘图函数之后调用plt.legend()
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)