Enrollments Open for Certification Course on Artificial Intelligence and Deep Learning by IIT Roorkee

  Apply Now

Getting Stared with Matplotlib - Create 3D Projections using Matplotlib

Creating 3D projections in Matplotlib is very simple. You need to import Axes3D, which registers the "3d" projection. Then create a subplot setting the projection to "3d". This returns an Axes3DSubplot object, which you can use to call plot_surface, giving x, y, and z coordinates, plus optional attributes.

INSTRUCTIONS
  • Import Axes3D:

    from mpl_toolkits.mplot3d import << your code goes here >>
    
  • Import other libraries

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib
    
  • Create the projection:

    x = np.linspace(-5, 5, 50)
    y = np.linspace(-5, 5, 50)
    X, Y = np.meshgrid(x, y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)
    

    Here, linspace returns evenly spaced numbers over a specified interval. meshgrid returns coordinate matrices from coordinate vectors.

  • Finally, we will create the figure, the subplot, and call the plot_surface with the coordinates we created earlier:

    figure = plt.figure(1, figsize = (12, 4))
    subplot3d = plt.subplot(111, projection='3d')
    surface = subplot3d.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=matplotlib.cm.coolwarm, linewidth=0.1)
    plt.show()
    

    The figure module provides the top-level Artist , the Figure , which contains all the plot elements. The figsize marks the width and height of the plot.


No hints are availble for this assesment

Answer is not availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...