python - Seaborn 无法识别列名称

标签 python pandas matplotlib seaborn

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

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

现在我尝试用另一个数据帧来做一些事情

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]

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

最佳答案

你必须命名 pandas 的系列:

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

关于python - Seaborn 无法识别列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42455934/

相关文章:

Python Sparql 查询本地文件

python - 为什么 pandas 中的 DataFrame 在字符串之前排列数字?

python - matplotlib: AttributeError: 'AxesSubplot' 对象没有属性 'add_axes'

python - 如何删除绘图上的单个刻度标签,保留刻度本身

python - 在matplotlib中绘制相关图

python - 如何根据另一个数据帧中的(日期)值过滤数据帧

python - Seaborn FacetGrid 子图的一个共享 x 轴标签(布局/间距?)

python - Altair:从偏移量向下指向的条形图

python - python 中的数据类型(寻找类似于 R 中的 str 的东西)

python - Pandas - 计算具有可变窗口大小的滚动累积乘积