Python Matplotlib 3D Plot
In this post, you will learn how to generate 3D plot using Python Matplotlib.
A plot can present the data in continuous, discrete, surface or volume form. Matplotlib was initially designed with only two-dimensional plotting in mind. In the release of 1.0 version, the three-dimensional plotting utilities were built on the top of Matplotlib's two-dimensional display, and the result is helpful for three-dimensional data visualization.
The 3D scatter plots are used to plot data points on three axes in an attempt to show the relationship between three variables. In this, we compare three data sets. Python provides many libraries to plot 3D graphs. In this article, we will use the submodule Axes3D of the Matplotlib library. We can generate many types of 3D plots using this module, like- 3D line plots, 3D scatter plots, 3D surface plots and much more.
Import Modules
To create any type of plot, first we need to import the necessary modules. 3D plots are enabled by importing the mplot3d toolkit, which is included with the main Matplotlib installation.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
Matplotlib Figure Object (plt.figure())
The Matplotlib provides plt.figure() to create a figure object. With this object, we will create a subplot and add a projection attribute of type 3D. It has several parameters that determine what the figure will look like. Here, an Axes3D object is created using the projection='3d' keyword.
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
Matplotlib 3D Scatter Plot
The idea of 3D scatter plot is that we can compare three characteristics of a data set instead of two. It is created from sets of (x, y, z) triples. Here is the full code for the 3D scatter plot using the Matplotlib Axes3D.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x =[1,2,3,4,5,6,7,8,9,10]
y =[15,11,10,3,13,5,3,7,2,12]
z =[7,8,9,4,5,6,11,13,10,16]
ax.scatter(x, y, z, c='r', marker='o')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
Output of the above code:
Matplotlib 3D Bar Plots
Bar plot represents data in rectangular bars with heights proportional to the values that they represent. It is used to compare things between different groups or to track changes over time. The bar graphs are best when there are big changes in data over time. Here is the full code of the 3D bar plot using Matplotlib Axes3D, which compares 3 characteristics of a data set.
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [10,2,5,9,12,6,1]
y = [15,11,10,3,13,5,3]
_xx, _yy = np.meshgrid(x, y)
a, b = _xx.ravel(), _yy.ravel()
top = a + b
bottom = np.zeros_like(top)
width = depth = 1
ax.bar3d(a, b, bottom, width, depth, top, color='yellow')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
Output of the above code:
Matplotlib 3D Surface plots
A 3D surface plot is a three-dimensional graph that is useful for investigating desirable response values and operating conditions.
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
# Defining surface and axes
x = np.outer(np.linspace(-4, 4, 10), np.ones(10))
y = x.copy().T
z = np.cos(x ** 2 + y ** 3)
fig = plt.figure()
# Syntax for 3-D plotting
ax = plt.axes(projection ='3d')
# Matplotlib plotting
ax.plot_surface(x, y, z, cmap ='viridis', edgecolor ='red')
ax.set_title('Surface 3D plot etutorialspoint')
plt.show()
Output of the above code:
Matplotlib 3D Wireframe plots
The wireframes 3D plots are based on gridded data. The wireframe plot basically works on gridded data and projects it onto the specified 3-dimensional surfaces, and it can help in making the resulting three-dimensional forms quite easy for visualization.
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
# function for z axea
def f(x, y):
return np.sin(np.sqrt(x ** 2 + y ** 2))
# x and y axis
x = np.linspace(-1, 5, 10)
y = np.linspace(-1, 5, 10)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig = plt.figure()
ax = plt.axes(projection ='3d')
ax.plot_wireframe(X, Y, Z, color ='red')
ax.set_title('Wireframe 3D plots');
plt.show()
Output of the above code:
Matplotlib 3D Contour Graphs
A contour plot is a graphical technique for representing a 3-dimensional surface by plotting constant z-slices, called contours, in a 2-dimensional format. A contour plot is also called a line plot.
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
# function for z axis
def f(x, y):
return np.sin(np.sqrt(x ** 2 + y ** 3))
# x and y axis
x = np.linspace(-4, 4, 10)
y = np.linspace(-4, 4, 10)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig = plt.figure()
ax = plt.axes(projection ='3d')
# ax.contour3D is used plot a contour graph
ax.contour3D(X, Y, Z);
plt.show()
Output of the above code:
Related Articles
Simple Matplotlib Animation ExampleMatplotlib Pie Chart
Python Line Plot Using Matplotlib
Python Matplotlib Bar Plot
How to save figure in matplotlib pyplot
Python Matplotlib Scatter Plot
Python Pandas Plotting
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Human detection OpenCV
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples