# Plotting 3D-graphs in Python using matplotlib

Today we’ll learn about plotting 3D-graphs in Python using matplotlib. Matplotlib is an amazing module which not only helps us visualize data in 2 dimensions but also in 3 dimensions. 3D graphs represent 2D inputs and 1D output. The submodule we’ll be using for plotting 3D-graphs in python is mplot3d which is already installed when you install matplotlib. So, you need to make sure you have installed matplotlib to implement this tutorial.

So, Let’s get started!

Imports:

In this tutorial, we will be using the 3D plots in matplotlib. There are also other options like pandas3D. Feel free to play around with that too.

The submodule of matplotlib called mpl_toolkits is used to plot our 3D graphs. Check out its documentation here. We will also import matplotlib.pyplot itself too.

from mpl_toolkits import mplot3d import matplotlib.pyplot as plt %matplotlib inline

## Making our dataset for 3d graph plotting

Now we need to get our x, y, and z values so that we can plot them. You can also make use of a csv or excel dataset to make it easier to visualize. Here, we’ll create three numpy arrays representing x, y, and z values.

For this, first import numpy and randint() function to create random datavalues:

import numpy as np from random import randint

We then define our numpy arrays by using the randint() function and list comprehension.

x=np.array([randint(0,100) for x in range(10)]) y=np.array([randint(0,100) for x in range(10)]) z=np.array([randint(0,100) for x in range(10)])

Now, let’s see what our values are :

print(x) print(y) print(z)

Output:

How do I plot in 3D

a polyhedron using 3d vectors as vertices and arrays of vectors as faces