简体   繁体   中英

How can I get a line fit data of distplot of seaborn in for loop?

I try to get the line data of distplot in seaborn in for loop. but when I execute this code it prints same values even though all elements of list X are different. please let me know why this happen

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()[0].get_data()
    print(dist_data_1)


You are plotting all the distribution on the same axes, so you are adding new lines at each iteration. However, you are always retrieving the data from the first line (index [0] ).

Instead, you should retrieve the data from the last line (index [-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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM