简体   繁体   English

向 seaborn 二维直方图添加正态分布

[英]Add a normal distribution to seaborn 2D histogram

Is it possible to take a histogram from seaborn and add a normal distribution?是否可以从 seaborn 获取直方图并添加正态分布?

Say I had something like this scatter plot and histogram from the documentation.假设我有类似的散点图 plot 和文档中的直方图。

import seaborn as sns
penguins = sns.load_dataset("penguins")
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm");
plt.savefig('deletethis.png', bbox_inches='tight')

在此处输入图像描述

Can i superimpose a distribution on the sides like the image below?我可以像下图那样在侧面叠加分布吗?

在此处输入图像描述

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm

x = np.random.normal(size=100000)

# Plot histogram in one-dimension
plt.hist(x,bins=80,density=True)
xvals = np.arange(-4,4,0.01)
plt.plot(xvals, norm.pdf(xvals),label='$N(0,1)$')
plt.legend();

The following gives a Kernel Density Estimate which displays the distribution (and if it is normal):下面给出了一个 Kernel 密度估计,它显示了分布(如果它是正常的):

g = sns.JointGrid(data=penguins, x="bill_length_mm", y="bill_depth_mm")
g.plot_joint(sns.scatterplot, s=100, alpha=.5)
g.plot_marginals(sns.histplot, kde=True)

The following superimposes a normal distribution on the histograms in the axes.下面在轴上的直方图上叠加一个正态分布。

import seaborn as sns
import numpy as np
import pandas as pd
from scipy.stats import norm

df1 = penguins.loc[:,["bill_length_mm", "bill_depth_mm"]]

axs = sns.jointplot("bill_length_mm", "bill_depth_mm", data=df1)
axs.ax_joint.scatter("bill_length_mm", "bill_depth_mm", data=df1, c='r', marker='x')

axs.ax_marg_x.cla()
axs.ax_marg_y.cla()
sns.distplot(df1.bill_length_mm, ax=axs.ax_marg_x, fit=norm)
sns.distplot(df1.bill_depth_mm, ax=axs.ax_marg_y, vertical=True, fit=norm)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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