gpt4 book ai didi

python - Seaborn 不识别列名

转载 作者:太空宇宙 更新时间:2023-11-04 00:39:34 25 4
gpt4 key购买 nike

这些是我使用 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)

现在我尝试用另一个 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]

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

最佳答案

你必须给 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com