英文:
seaborn plot multivariate normal distribution
问题
以下是翻译好的部分:
以下代码尝试使用seaborn绘制多变量正态分布:
# 设置均值和协方差
mean1 = [0, 0]
mean2 = [2, 0]
cov1 = [[1, .7], [.7, 1]]
cov2 = [[.5, .4], [.4, .5]]
# 从均值和协方差生成数据
data1 = np.random.multivariate_normal(mean1, cov1, size=1000)
data2 = np.random.multivariate_normal(mean2, cov2, size=1000)
plt.figure(figsize=(10, 6))
plt.scatter(data1[:, 0], data1[:, 1])
plt.scatter(data2[:, 0], data2[:, 1])
sns.kdeplot(data1[:, 0], data1[:, 1], levels=20, linewidth=10, color='k', alpha=0.2)
sns.kdeplot(data2[:, 0], data2[:, 1], levels=20, linewidth=10, color='k', alpha=0.2)
plt.grid(False)
plt.show()
它引发错误:
TypeError: kdeplot()需要0到1个位置参数,但给定了2个位置参数(以及2个仅关键字参数)。
请问如何解决这个问题?最好的问候。
英文:
The following code tries to plot multivariate normal distribution using seaborn:
# Set the mean and covariance
mean1 = [0, 0]
mean2 = [2, 0]
cov1 = [[1, .7], [.7, 1]]
cov2 = [[.5, .4], [.4, .5]]
# Generate data from the mean and covariance
data1 = np.random.multivariate_normal(mean1, cov1, size=1000)
data2 = np.random.multivariate_normal(mean2, cov2, size=1000)
plt.figure(figsize=(10,6))
plt.scatter(data1[:,0],data1[:,1])
plt.scatter(data2[:,0],data2[:,1])
sns.kdeplot(data1[:, 0], data1[:, 1], levels=20, linewidth=10, color='k', alpha=0.2)
sns.kdeplot(data2[:, 0], data2[:, 1], levels=20, linewidth=10, color='k', alpha=0.2)
plt.grid(False)
plt.show()
it raises error:
TypeError: kdeplot() takes from 0 to 1 positional arguments but 2 positional arguments (and 2 keyword-only arguments) were given
Kindy advise how can this be achieved?
Best regards
答案1
得分: 1
错误告诉您问题的确切原因。请查看文档中的函数签名:
seaborn.kdeplot(data=None, *, x=None, y=None, ...)
data
可以作为关键字参数或位置参数传递,而在 *
之后的参数仅限于关键字参数。因此,您应该明确指定 x=
和 y=
:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 设置均值和协方差
mean1 = [0, 0]
mean2 = [2, 0]
cov1 = [[1, .7], [.7, 1]]
cov2 = [[.5, .4], [.4, .5]]
# 从均值和协方差生成数据
data1 = np.random.multivariate_normal(mean1, cov1, size=1000)
data2 = np.random.multivariate_normal(mean2, cov2, size=1000)
plt.figure(figsize=(10, 6))
plt.scatter(data1[:, 0], data1[:, 1])
plt.scatter(data2[:, 0], data2[:, 1])
sns.kdeplot(x=data1[:, 0], y=data1[:, 1], levels=20, linewidth=10, color='k', alpha=0.2)
sns.kdeplot(x=data2[:, 0], y=data2[:, 1], levels=20, linewidth=10, color='k', alpha=0.2)
plt.grid(False)
plt.show()
英文:
The error tells you exactly what the problem is. Take a look at the function signature in the documentation:
seaborn.kdeplot(data=None, *, x=None, y=None, ...)
data
can be passed as kwarg or positional argument, whereas arguments after *
are kwargs only. You should therefore specify x=
and y=
:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Set the mean and covariance
mean1 = [0, 0]
mean2 = [2, 0]
cov1 = [[1, .7], [.7, 1]]
cov2 = [[.5, .4], [.4, .5]]
# Generate data from the mean and covariance
data1 = np.random.multivariate_normal(mean1, cov1, size=1000)
data2 = np.random.multivariate_normal(mean2, cov2, size=1000)
plt.figure(figsize=(10,6))
plt.scatter(data1[:,0],data1[:,1])
plt.scatter(data2[:,0],data2[:,1])
sns.kdeplot(x=data1[:, 0], y=data1[:, 1], levels=20, linewidth=10, color='k', alpha=0.2)
sns.kdeplot(x=data2[:, 0], y=data2[:, 1], levels=20, linewidth=10, color='k', alpha=0.2)
plt.grid(False)
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论