Seaborn不识别列名

时间:2017-02-25 12:15:41

标签: python pandas matplotlib seaborn

这是我对seaborn的第一步:我正在尝试使用lmplot方法。 Here有一个看起来很容易理解的例子,我报告了一些没有"的代码; sns.set(color_codes =真)"第一行的指示,因为它看起来很重要

import seaborn as sns
tips = sns.load_dataset("tips")
g = sns.lmplot(x="total_bill", y="tip", data=tips)

现在我尝试用另一个dataFrame来做一些

x = np.arange(-3, 3, 0.01)
a = 5
b = 3
rand = np.random.randn(len(x))
y = a + b*x + rand
xS = pd.Series(x)
yS = pd.Series(y)
data = pd.concat( [ xS, yS ], axis = 1)
g = sns.lmplot(x = 'x', y = 'y', data = data)

但是错误的信息对我来说很奇怪。

Traceback (most recent call last):

  File "<ipython-input-41-20aa1710b84e>", line 1, in <module>
    g = sns.lmplot(x = 'x', y = 'y', data = data)

  File "C:\Users\fedel\Anaconda2c\lib\site-packages\seaborn\linearmodels.py", line 541, in lmplot
    data = data[cols]

有没有人可以帮我解决这个问题?

2 个答案:

答案 0 :(得分:2)

你必须为熊猫系列命名:

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

x = np.arange(-3, 3, 0.01)
a = 5
b = 3
rand = np.random.randn(len(x))
y = a + b*x + rand
xS = pd.Series(x, name='x')
yS = pd.Series(y, name='y')
data = pd.concat( [ xS, yS ], axis = 1)
g = sns.lmplot(x = 'x', y = 'y', data = data)
plt.show()

enter image description here

答案 1 :(得分:1)

对不起,这只是一个疏忽。这里的代码是

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

x = np.arange(-3, 3, 0.01)
a = 5
b = 3
rand = np.random.randn(len(x))
y = a + b*x + rand
xS = pd.Series(x)
yS = pd.Series(y)
d = pd.concat( [ xS, yS ], axis = 1)
d.columns = [ 'x axis', 'y axis']
g = sns.lmplot(x = 'x axis', y = 'y axis', data = d)
plt.show()