我嘗試在for循環(huán)中獲取seaborn中distplot的行數(shù)據(jù)。但是當我執(zhí)行此代碼時,即使列表 X 的所有元素都不同,它也會打印相同的值。請告訴我為什么會發(fā)生這種情況import numpy as npimport seaborn as sns import matplotlib.pyplot as plt x1 = np.random.normal(10,3,size=100)x2 = np.random.normal(8,2,size=100)x3 = np.random.normal(7,2,size=100)x4 = np.random.normal(11,5,size=100)X = [x1, x2, x3, x4]for i in X: dist_data_1 = sns.distplot(i).get_lines()[0].get_data() print(dist_data_1)
1 回答

肥皂起泡泡
TA貢獻1829條經(jīng)驗 獲得超6個贊
您正在同一軸上繪制所有分布,因此您在每次迭代時添加新線。但是,您始終從第一行(索引)檢索數(shù)據(jù)[0]。
相反,您應(yīng)該從最后一行(索引)檢索數(shù)據(jù)[-1]:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
x1 = np.random.normal(10,3,size=100)
x2 = np.random.normal(8,2,size=100)
x3 = np.random.normal(7,2,size=100)
x4 = np.random.normal(11,5,size=100)
X = [x1, x2, x3, x4]
for i in X:
dist_data_1 = sns.distplot(i).get_lines()[-1].get_data()
print(dist_data_1)
添加回答
舉報
0/150
提交
取消