Python

Seaborn 3D Plot

We may plot different patterns of 3D graphs with the configuration of the three-dimensional coordinates. One of the features that incentives us from seeing the graphs dynamically is the 3D graphing; we need it when using the immersive graphics. We may import the necessary libraries, which include some functions for creating the three-dimensional graphs. Since there is no inbuilt functionality in the Seaborn module to draw the 3D graphs, it’s the Matplotlib plugin that depends on to do the actual work in the three-dimensional graphs. Therefore, we may use Seaborn to customize the 3D graphics. We will explore how to draw numerous different three-dimensional graphs in this tutorial.

Example 1

A group of scatter graphs formed using the combinations of triples is the most basic 3D graph. The plot3D() function and the scatter3D() method, like the more frequent 2D charts, are used to draw these. In this case, we’ll draw a hyperbolic helix, with various spots close to the line.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

fig = plt.figure()

ax = plt.axes(projection='3d')
zline = np.linspace(0, 20, 2000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, 'gray')

zdata = 15 * np.random.random(200)
xdata = np.sin(zdata) + 0.1 * np.random.randn(200)
ydata = np.cos(zdata) + 0.1 * np.random.randn(200)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Blues');

plt.show()

Here, we import the required header files. NumPy module is introduced as np, matplotlib.pyplot is introduced as plt, and Seaborn is introduced as sns. In the next step, we call the figure() function to draw the figure. This function belongs to matplotlib.pyplot library. Now, we utilize the axes() function to specify the value of projection.

Here, we want to create a 3-Dimensional plot, so we have given a 3D value. Let us define the data for a 3D line. We declare the three different variables and assign them to the values for all the three axes. First, we set the values for spacing for the z-axis, so we apply the linespace() function of the NumPy package. With the help of the value of z-axis spacing, we indicate the values of spacing for other axes, x and y.

Now, to draw a 3D plot, we employ the plot3D() function. Here, we provide the spacing of all axes and the color of the background of the 3D plot as the argument of the function. Furthermore, we set the data for the 3D scatter points. We separately call the randn() function of the NumPy library for all of the three axes. By doing this, we get the points from which we draw the scatter graph.

To draw the scatter plot by using these points, we utilize the scatter3D() method. This function contains several parameters which include data of x, y, and z-axis as well as the value of “cmap”. The value of the parameter “cmap” shows the color of the line drawn in the 3D plot. The show() method is called to illustrate the map.

Example 2

All data required for the contour3D() method must be in the condition of a 2D grid nodes, as well as the Z data analyzed at every point. A 3D contour graph of a periodic function is shown in the following illustration:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

def f(x, y):
    return np.sin(np.sqrt(x ** 4 + y ** 4))

x = np.linspace(-4, 4, 25)
y = np.linspace(-4, 4, 25)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 40, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

plt.show()

At the start of the program, we incorporate the libraries NumPy as np, matplotlib.pyplot as plt, and Seaborn as sns. Now, we define an f()function having two parameters that return the value by using the sqrt() method of the NumPy library. We indicate the values of spacing for both axes x and y with the help of linspace().This method is related to the NumPy framework.

In the next line, we have apply the meshgrid() function to create a mesh grid in the 3D graph. This function holds the value of line spacing of x and y axes as its parameters. Here, we use the figure() function of matplotlib.pyplot library to create the figure. To define the level of projection, we use the axes() method. We must construct a three-dimensional map, so we provide a 3D value to the function. We now apply the contour3D() method.

As the function’s parameter, we give it the spacing of all axes and the shade of the 3D graph. Next, we specify the captions of all the three axes so the set_label() method is called for all axes respectively.  To demonstrate the chart, the show() function is used.

Example 3

In this instance, we create a surface plot in the three-dimensional graph by using the plot_surface() method.

import seaborn as sb
import matplotlib.pyplot as plot
import numpy as np

def FUNC_Z(x, y):
    return50 - (x**2 + y**2)

sb.set_style('whitegrid')
N = 50
X_VAL = np.linspace(-5, 5, N)
Y_VAL = np.linspace(-5, 5, N)

X1, Y1 = np.meshgrid(X_VAL, Y_VAL)

Z1 = FUNC_Z(X1, Y1)
axes = plot.axes (projection='3d')
axes.plot_surface(X1, Y1, Z1)

plot.show()

First of all, we integrate the modules Seaborn, matplotlib.pyplot, and np. We define the function for the z-axis and this function is provided with the x and y-axis. This function returns the value of the z-axis. In the next phase, we employ the set_style() method to customize the layout of the plot. We set its value to whitegrid.

Now, we declare a variable and assign it a value. The linspace() method is called to indicate the spacing of the x and y-axis. We draw the meshgrid to the z-axis, so we utilize the meshgrid() method. This function is taken from the NumPy package. We call the function for the values of the z-axis. For the 3D plot, it is mandatory to specify the projection value. For this, we employ the axes() function.

To draw the surface graph, the plot_surface() function is executed. This function contains the value of all the three axes. Before terminating the code, we use the show() method to depict the finalized plot.

Example 4

Here, we use the wireframe() method to draw the wireframe in the 3D plot.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

fig = plt.figure()
ax = plt.axes(projection='3d')
zline = np.linspace(0, 20, 2000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, 'gray')
zdata = 15 * np.random.random(200)
xdata = np.sin(zdata) + 0.1 * np.random.randn(200)
ydata = np.cos(zdata) + 0.1 * np.random.randn(200)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Blues')

plt.show()

After introducing the essential frameworks, we identify the z-axis feature which is available with the x and y axes. The z-axis value is obtained by this process. We use the set style() function to modify the graph’s design in the next step. Its value is configured to the whitegrid. We now initialize a variable N and give it a value.

To specify the spacing between the x and y axes, the linspace() technique is applied. We use the meshgrid() function because we want to depict the meshgrid to the z-axis. This is a technique of the NumPy toolkit. The component for the z-axis value is considered. The projection value must be stated for the 3D visualization. We use the axes() method for this. The wireframe() method is applied to create the wireframe chart. The value of all the three axes is incorporated in this method. We employ the show() function to display the entire graph in the end.

Conclusion

In this section, we have gone all the way through different approaches used to create a 3D plot with the Seaborn framework. We also learned how to draw the 3D contour maps, how to draw the wireframe on the three-dimensional chart, and how to create a surface graph on the 3D plots. There is no integrated three-dimensional feature in the Seaborn framework.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content