How to visualize the data set in 3D scatter plot using matplot library in python?


To visualize the data set in 3D representation using python.

  Import mpl_toolkit and related 3D objects from matplot library.

  Read the sample data.

  Initialize the figure object(add_subplot()).

  Initialize the x,y,z axis.

  Set the x-label,y-label and z-label.

#import 3D tools and objects

from mpl_toolkits.mplot3d import axes3d

#import matplot library

import matplotlib.pyplot as plt

import pandas as pd

#sample data set


300,200,100, 50],’age’





,850,250,750, 1000]}

#store in a data frame


#initialize the figure object

fig = plt.figure()

ax1 = fig.add_subplot(111, projection=’3d’)

#passing the variables in to

corresponding axis

x = df[‘salary’]

y = df[‘age’]

z = df[‘bonus’]

a = df[‘rating’]

#3D scatter plot

ax1.scatter(x, y, z, c=’g’, marker=’o’)

ax1.scatter(x, y, a, c=’r’, marker=’o’)

ax1.set_xlabel(‘x axis’)

ax1.set_ylabel(‘y axis’)

ax1.set_zlabel(‘z axis’)

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit