I would like to change the contour plot that I have below for a 2-D Gaussian distribution to a 3-D surface plot. I have my code below along with the plot. Not sure how to change it to the 3-D version. Does anyone have any tips? Note: I actually have four contour plots and I would like to change them all into 3-D surface plots.
import pandas as pd
import math
import cmath
import time
import random
import io
import itertools
import requests
import statistics
import numpy as np
from scipy.stats import multivariate_normal
import plotly.express as px
from plotly.subplots import make_subplots
import warnings
from IPython.display import HTML, display
import tabulate
import plotly.graph_objects as go
def norm_pdf_multivariate(x, mu, sigma):
size = len(x)
det = np.linalg.det(sigma)
norm_const = 1.0/ ( math.pow((2*math.pi),float(size)/2) * math.pow(det,1.0/2) )
x_mu = np.matrix(x - mu)
inv = np.linalg.inv(sigma)
result = math.pow(math.e, -0.5 * (x_mu * inv * x_mu.T))
return norm_const * result
x = np.linspace(-6,6,100)
y = np.linspace(-6,6,100)
X, Y = np.meshgrid(x, y)
X_ = X.tolist()
X_ = [y for x in X_ for y in x]
Y_ = Y.tolist()
Y_ = [y for x in Y_ for y in x]
Samples = [[X_[i],Y_[i]] for i in range(len(X_))]
Cov_1 = [[1,0],[0,1]]
Means_1 = [0,0]
Cov_2 = [[1,0],[0,5]]
Means_2 = [0,0]
Cov_3 = [[5,1],[1,1]]
Means_3 = [0,0]
Cov_4 = [[5,-1],[-1,1]]
Means_4 = [0,0]
Z_1 = []
Z_2 = []
Z_3 = []
Z_4 = []
for i in range(len(Samples)):
Z_1.append(norm_pdf_multivariate(x = np.array(Samples[i]),mu = Means_1,sigma = Cov_1))
Z_2.append(norm_pdf_multivariate(x = np.array(Samples[i]),mu = Means_2,sigma = Cov_2))
Z_3.append(norm_pdf_multivariate(x = np.array(Samples[i]),mu = Means_3,sigma = Cov_3))
Z_4.append(norm_pdf_multivariate(x = np.array(Samples[i]),mu = Means_4,sigma = Cov_4))
colorscale = [[0, 'gold'], [0.5, 'mediumturquoise'], [1, 'lightsalmon']]
fig = make_subplots(rows = 4,cols = 1)
fig.append_trace(go.Contour(z=Z_1,
x=X_,
y=Y_,
contours_coloring='lines',
colorscale = colorscale,
line_width=2,
showscale=False),row = 1,col = 1)
fig.append_trace(go.Contour(z=Z_2,
x=X_,
y=Y_,
contours_coloring='lines',
colorscale = colorscale,
line_width=2,
showscale=False),row = 2,col = 1)
fig.append_trace(go.Contour(z=Z_3,
x=X_,
y=Y_,
contours_coloring='lines',
colorscale = colorscale,
line_width=2,
showscale=False),row = 3,col = 1)
fig.append_trace(go.Contour(z=Z_4,
x=X_,
y=Y_,
contours_coloring='lines',
colorscale = colorscale,
line_width=2,
showscale=False),row = 4,col = 1)
fig.update_layout(
width = 350,
height = 1200
)
fig.update_layout(template = "plotly_white")
fig.show()