Plotting 3D scatter plots

In this recipe, we will learn how to plot 3D scatterplots and visualize them in three dimensions.

How to do it…

  1. Create a new Python file, and import the following packages:
    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
  2. Create the empty figure:
    # Create the figure
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
  3. Define the number of values that we should generate:
    # Define the number of values
    n = 250
  4. Create a lambda function to generate values in a given range:
    # Create a lambda function to generate the random values in the given range
    f = lambda minval, maxval, n: minval + (maxval - minval) * np.random.rand(n)
  5. Generate X, Y, and Z values using this function:
    # Generate the ...

Get Python Machine Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.