【发布时间】:2021-11-26 07:24:34
【问题描述】:
我试图绘制一个 seaborn distplot。
示例代码:
import pandas as pd
import seaborn as sns
import numpy as np
import scipy
import matplotlib.pyplot as plt
# data
np.random.seed(365)
x1 = np.random.normal(10, 3.4, size=1000) # mean of 10
df = pd.DataFrame({'x1': x1})
def map_pdf(x, **kwargs):
mu, std = scipy.stats.norm.fit(x)
x0, x1 = p1.axes[0][0].get_xlim() # axes for p1 is required to determine x_pdf
x_pdf = np.linspace(x0, x1, 100)
y_pdf = scipy.stats.norm.pdf(x_pdf, mu, std)
plt.plot(x_pdf, y_pdf, c='r')
p1 = sns.displot(data=df, x='x1', kind='hist', bins=40, stat='density')
p1.map(map_pdf, 'x1')
不知道为什么执行上述代码后我没有得到任何输出!
执行上述代码后,我得到了这个,
<seaborn.axisgrid.FacetGrid at 0x7f6a6fa0f820>
对此的任何帮助将不胜感激。
提前感谢您的支持!
【问题讨论】:
-
此代码适用于
seaborn 0.11.2和matplotlib 3.4.3。见code and plot -
如果您使用的是 Jupyter Notebook,请将
%matplotlib inline添加到您的导入中并重试 -
是的,我可以给某人指点新的FAQ page :)
标签: python-3.x pandas seaborn