繁体   English   中英

如何在 y 轴上绘制高斯分布?

[英]How to plot a Gaussian Distribution on y-axis?

我希望每个人都健康并保持安全。

我目前有以下情节零均值 . 我想最终在 x=0, y=0 处绘制这样的高斯分布。 橙色线基本上是 95% 的置信区间。: x=0, y=0 处的高斯分布

我应该尝试交换轴并绘图吗? 有没有更好的方法来做到这一点? 我目前正在使用 matplotlib 和 python 进行绘图。 有更好的库可以绘制吗? 请告诉我。

谢谢!

我有以下代码:

 import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.gaussian_process import GaussianProcessRegressor from sklearn.gaussian_process.kernels import RBF, ConstantKernel noise = 1.0 X = np.arange(0, 1, 0.01).reshape(-1, 1) kernel = ConstantKernel(10**2) * RBF(length_scale=0.35) gp = GaussianProcessRegressor(kernel=kernel, alpha=noise**2, optimizer=None) gp_mean, gp_std = gp.predict(X, return_std=True) # Create the figure and the axes fig, ax = plt.subplots() ax.plot(X, gp_mean, 'k-', label='Zero-Mean GP') ax.fill_between(X.ravel(), gp_mean + 1.96*gp_std, gp_mean - 1.96*gp_std, alpha=0.30, label='95% confidence interval') ax.grid() ax.legend(prop={'size': 12}) ax.set_xlim([-0.02, 1.0]) ax.set_ylim([-30.0, 30.0]) ax.tick_params(axis='both', labelsize=14) ax.set_xlabel(r'$x$', fontsize=14) plt.show()

您可以在 y 轴上绘制高斯法线的 pdf,如下所示:

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

gp_mean = 0
gp_std = 12
gaussian = stats.norm(gp_mean, gp_std)

fig, ax = plt.subplots()
ys = np.linspace(*gaussian.ppf([0.001, 0.999]), 200)
ax.plot(gaussian.pdf(ys), ys, color='deepskyblue', label='gaussian normal')
ax.axhspan(*gaussian.ppf([0.05, 0.95]), color='chocolate', alpha=0.2, label='95% confidence interval')
ax.plot(0, gp_mean, marker='o', color='crimson', label='mean')
ax.set_xlim(0, 0.5)
ax.legend(prop={'size': 12})
plt.show()

结果图

PS:还要在x = 0.5处绘制 pdf 和平均值,您可以添加:

ax.plot(0.5 + gaussian.pdf(ys), ys, color='deepskyblue')
ax.plot(0.5, gp_mean, marker='o', color='crimson')
ax.set_xlim(0, 1)

暂无
暂无

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

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