How to plot a Gaussian Distribution on y-axis?

Viewed 356

I hope everyone's healthy and keeping safe.

I currently have the following plotzero-mean. I want to ultimately plot a Gaussian distribution like this at x=0, y=0. The orange lines are basically the 95% confidence interval.: A Gaussian distribution at x=0, y=0

Should I try swapping the axis and plotting? Is there a better way to do it? I am currently plotting in matplotlib and python. Are there better libraries to plot? Please let me know.

Thank you!

I have the following code:

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()

1 Answers

You could draw the pdf of a gaussian normal on the y-axis as follows:

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()

resulting plot

PS: To also draw the pdf and mean at x = 0.5, you can add:

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)
Related