在直方图上添加密度曲线 [英] Add density curve on the histogram
本文介绍了在直方图上添加密度曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我能够在 python 中制作直方图,但我无法添加密度曲线,我看到许多代码使用不同的方式在直方图上添加密度曲线,但我不知道如何使用我的代码
我添加了 density = true 但无法在直方图上获得密度曲线
df = pd.DataFrame(np.random.randn(100,4),columns = list('ABCD'))X = df ['A']hist,bins = np.histogram(X,bins = 10,density = True)宽度 = 0.7 * (bins[1] - bins[0])中心=(bins [:-1] + bins [1:])/2plt.bar(center, hist, align='center', width=width)plt.show()
解决方案
以下是使用
但是, distplot
将
sns.displot
将pandas导入为pd导入matplotlib.pyplot作为plt将 seaborn 作为 sns 导入将numpy导入为npdf = pd.DataFrame(np.random.randn(100,4),columns = list('ABCD'))X = df ['A']sns.displot(X, kde=True, bins=20)plt.show()
I am able to make histogram in python but I am unable to add density curve , I see many code which are using different ways to add density curve on histogram but I am not sure how to get on my code
I have added density = true but not able to get density curve on histogram
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()
解决方案
Here 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()
However, distplot
will be removed in a future version of seaborn. Therefore, alternatives are to use histplot
and displot
.
sns.histplot
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.histplot(X, kde=True, bins=20)
plt.show()
sns.displot
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.displot(X, kde=True, bins=20)
plt.show()
这篇关于在直方图上添加密度曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文