【问题标题】:No output for seaborn distplotseaborn distplot 没有输出
【发布时间】: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.2matplotlib 3.4.3。见code and plot
  • 如果您使用的是 Jupyter Notebook,请将 %matplotlib inline 添加到您的导入中并重试
  • 是的,我可以给某人指点新的FAQ page :)

标签: python-3.x pandas seaborn


【解决方案1】:

使用plt.show 显示您的绘图。解决方案在下面重新创建并提供了相同的内容。

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')
plt.show(p1)

【讨论】:

  • @adey27,您能否通过上述解决方案解决您的问题?
  • 谢谢.. 添加 '%matplotlib inline' 后它确实有效
猜你喜欢
  • 1970-01-01
  • 2016-08-05
  • 2018-02-11
  • 2018-02-13
  • 2019-08-03
  • 2019-04-19
  • 2017-10-12
  • 2015-10-20
相关资源
最近更新 更多