Dex*_*611 5 python numpy matplotlib pandas seaborn
我可以在 python 中制作直方图,但无法添加密度曲线,我看到许多代码使用不同的方式在直方图上添加密度曲线,但我不确定如何获取我的代码
我添加了密度 = true 但无法在直方图上获得密度曲线
df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))
X=df['A']
hist, bins = np.histogram(X, bins=10,density=True)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.show()
Run Code Online (Sandbox Code Playgroud)
distplot has been removed: removed in a future version of seaborn. Therefore, alternatives are to use histplot and displot.
sns.histplotimport pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))
X = df['A']
sns.histplot(X, kde=True, bins=20)
plt.show()
Run Code Online (Sandbox Code Playgroud)
sns.displotimport pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))
X = df['A']
sns.displot(X, kde=True, bins=20)
plt.show()
Run Code Online (Sandbox Code Playgroud)
distplot has been removedHere is an approach using distplot method of seaborn. Also, mentioned in the comments:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))
X = df['A']
sns.distplot(X, kde=True, bins=20, hist=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)