Home > Back-end >  How to display a 3D plot in Python?
How to display a 3D plot in Python?

Time:01-14

I have the following code segment, but when I run it I only get a blank white screen and the plot is not displayed. I'm using Python 3.10.9. Any ideas about the issue?:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

def np_bivariate_normal_pdf(domain, mean, variance):
    X = np.arange(-domain mean, domain mean, variance)
    Y = np.arange(-domain mean, domain mean, variance)
    X, Y = np.meshgrid(X, Y)
    R = np.sqrt(X**2   Y**2)
    Z = ((1. / np.sqrt(2 * np.pi)) * np.exp(-.5*R**2))
    return X mean, Y mean, Z

def plt_plot_bivariate_normal_pdf(x, y, z):
    fig = plt.figure(figsize=(12, 6))
    ax = Axes3D(fig)
    ax.plot_surface(x, y, z,
                    cmap=cm.coolwarm,
                    linewidth=0,
                    antialiased=True)
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')
    plt.show()

def main():
    plt_plot_bivariate_normal_pdf(*np_bivariate_normal_pdf(4, 0, .25))

if __name__ == '__main__':
    main()

CodePudding user response:

ax = Axes3D(fig) was how to create 3D axes in Matplotlib prior to version 1.0.0. Since then, use fig.add_subplot(111, projection='3d'). See enter image description here

  • Related