In the previous tutorial, we have seen how to plot 2D graphs with matplotlib. Now we will see 3D plots with python and matplotlib. We will start with simple line plots and then we cover scatter plots, Surface plots, and many more plots.

## Basic 3D Plot With Python And Matplotlib

To start with 3D plotting we need two modules first one is Matplotlib and the second one is mpl-toolkits. We plotted 2D graphs with just only matplotlib but for 3D graphs we need mpl-toolkits.

Related Post : Plotting Line Graphs In Python Using Matplotlib

Now Let’s move towards 3D Plots With Python And Matplotlib.

**CODE:**

```
#Importing Libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
#3D Plotting
fig = plt.figure()
ax = plt.axes(projection="3d")
#Labeling
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
```

We can enable utility toolkit by importing the `mpl_toolkits.mplot3d`

library. You don’t need to download it because it comes with your standard Matplotlib installation via pip. Just be sure that your Matplotlib version is over 1.0.

After importing it we can start plotting 3D graphs by passing **projection=”3d”** in axes() function. Then we labeled our plot.

**OUTPUT:**

This is how you can plot a 3d graph in python.

## 3D Line Plots

Now we know how to plot simple 3D graph. So, let’s plot 3D line graph from a given data.

**CODE:**

```
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
ax = plt.axes(projection="3d")
x = [1,2,3,4,5,6,7,8,9]
y = [2,3,4,6,7,8,9,5,1]
z = [5,6,2,4,8,6,5,6,1]
ax.plot3D(x,y,z)
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
```

Here we have 3 lists x,y, and z. Which contains data and we have to plot it as a 3D line graph. We can simply do that with a function called **plot3D()**. Here you can see how it looks like:

**OUTPUT:**

## 3D Scatter Plot

3D scatter plots are one of the most popular 3-dimensional graphs. To plot a scatter graph we need to use **sctter()**. like we have used **plot3D** for the line graph.

We can also set color of scatter points. We have set color to **red** here.

**CODE:**

```
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
ax = plt.axes(projection="3d")
x = [1,2,3,4,5,6,7,8,9]
y = [2,3,4,6,7,8,9,5,1]
z = [5,6,2,4,8,6,5,6,1]
ax.scatter(x,y,z,color='r')
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
```

**OUTPUT:**

This is Scatter 3D plots with python and matplotlib.

## 3D Surface plots

Like line and scatter plots we can also plot surface graphs. All we have to use is **plot_surface()**.

By default it will be colored in shades of a solid color, but it also supports color mapping by supplying the ** cmap **argument.

Here z should be in 2-Dimension.

**CODE:**

```
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np
fig = plt.figure()
ax = plt.axes(projection="3d")
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
R = np.sqrt(x**2 + y**2)
z = np.sin(R)
ax.plot_surface(x,y,z)
ax.set_xlabel('X Axes')
ax.set_ylabel('Y Axes')
ax.set_zlabel('Z Axes')
plt.show()
```

**OUTPUT:**

This is Surface 3D plots with python and matplotlib.

## Other Some 3D Plots With Python And Matplotlib

- Tri-Surface plots
- Contour plots
- Filled contour plots
- Polygon plots
- Bar plots
- Wireframe plots

Get More About This From here : mplot3d