## Introduction to 3D Plotting

3D plotting is a powerful tool used in various fields such as engineering, physics, biology, and many more. It provides a visual representation of data, making it easier for us to understand complex relationships between variables.

In this article, we will explore what 3D plotting is, and how we can create them using Seaborn and Matplotlib libraries. What is a 3D plot?

A 3D plot, also known as a three-dimensional plot, is a graph that displays three variables of a dataset. These variables are mapped onto the x, y, and z-axis of the plot, where each axis represents a different variable.

This allows us to visualize the relationships between the variables in a 3D space.

## How to create a 3D plot using Seaborn and Matplotlib

In order to create 3D plots, we will be using the Seaborn and Matplotlib libraries. These libraries offer a wide variety of functions and tools to create 3D visualizations easily.

We will also be using the NumPy library to generate some sample data. First, we need to import the necessary libraries:

“`python

## import numpy as np

## import seaborn as sns

import matplotlib.pyplot as plt

## from mpl_toolkits import mplot3d

“`

## Creating a 3D Scatter Plot

## Creating axes for 3D plotting

Before we plot any data, we need to create axes for our 3D plot. This is done by creating a new figure object and specifying that it is a 3D plot using the projection parameter.

We can also set the figure size using the figsize parameter. “`python

fig = plt.figure(figsize=(8,8))

ax = plt.axes(projection=’3d’)

“`

## Using scatter3D() function to plot data points

Now that we have our axes set up, we can use the scatter3D() function to plot our data points. The scatter3D() function takes three parameters: x, y, and z, where x, y, and z are arrays of the same length that contain the data for each dimension.

“`python

x = np.random.normal(size=100)

y = np.random.normal(size=100)

z = np.random.normal(size=100)

ax.scatter3D(x, y, z);

“`

## Setting labels for 3 dimensions

To make our 3D plot more informative, we can add labels for each dimension. This is done using the set_xlabel(), set_ylabel(), and set_zlabel() functions.

“`python

ax.set_xlabel(‘X Label’)

ax.set_ylabel(‘Y Label’)

ax.set_zlabel(‘Z Label’)

“`

## Changing size of scatter points

We can also change the size of the scatter points using the s parameter. “`python

ax.scatter3D(x, y, z, s=50);

“`

## Rotating the 3D figure

To rotate the 3D figure, we can use the view_init() function. This function takes two parameters: elevation (elev) and azimuth angle (azim).

Elevation is the angle between the z-axis and the x-y plane, and azimuth is the angle between the x-axis and the y-axis. “`python

ax.view_init(elev=20, azim=30)

“`

## Conclusion

In this article, we explored the basics of 3D plotting and how to create a 3D scatter plot using Seaborn and Matplotlib libraries. We saw how we can create axes for 3D plotting, plot data points using the scatter3D() function, set labels for 3 dimensions, change the size of the scatter points, and rotate the 3D figure.

Hopefully, this article has provided you with a solid foundation for 3D plotting, and you can now create your own 3D visualizations to analyze your data.

## Creating a 3D Line Plot

While scatter plots allow us to visualize individual data points in 3D space, sometimes we need to visualize a continuous function instead. In these cases, we can use a 3D line plot using the plot3D() function in Matplotlib.

## Using plot3D() function to plot a line

The plot3D() function is used to plot lines in a 3D space. It takes three parameters: x, y, and z, where x, y, and z are arrays of the same length that contain the data for each dimension.

“`python

x = np.arange(-5, 5, 0.1)

y = np.sin(x)

z = np.cos(x)

ax.plot3D(x, y, z);

“`

This will create a 3D line plot of the sine and cosine functions on the x-axis.

## Creating a 3D Surface Plot

Surface plots are another type of 3D plot that allow us to visualize a continuous function in 3D space. In this section, we will create a 3D surface plot using the plot_surface() function in Matplotlib.

## Creating a function for Z values

To create a surface plot, we need to define a function that returns the Z values for each point in a 2D grid. For example, let’s define a function that returns the Z values for the equation z = x ** 2 – y ** 2.

“`python

def z_function(x, y):

return x ** 2 – y ** 2

“`

## Creating a grid using meshgrid()

Next, we need to create a grid of x and y values using the NumPy meshgrid() function. The meshgrid() function takes two 1D arrays and returns two 2D arrays: the first containing the x values and the second containing the y values.

“`python

x_values = np.arange(-5, 5, 0.1)

y_values = np.arange(-5, 5, 0.1)

x_grid, y_grid = np.meshgrid(x_values, y_values)

“`

## Using plot_surface() function to plot the surface

Now that we have our grid of x and y values, and a function that returns the corresponding z values, we can create our 3D surface plot using the plot_surface() function. “`python

z = z_function(x_grid, y_grid)

ax.plot_surface(x_grid, y_grid, z, cmap=plt.cm.viridis)

“`

This code will produce a surface plot of the equation z = x ** 2 – y ** 2.

## Changing the number of grid points for different aesthetics

The number of grid points we use can affect the quality and aesthetics of our surface plot. Generally speaking, the more grid points we have, the smoother the surface plot will look.

However, this comes at the cost of computational resources and rendering time. Here’s an example of how we can control the number of grid points for a better-looking surface plot:

“`python

x_values = np.linspace(-5, 5, 200)

y_values = np.linspace(-5, 5, 200)

x_grid, y_grid = np.meshgrid(x_values, y_values)

z = z_function(x_grid, y_grid)

fig = plt.figure(figsize=(12, 8))

ax = plt.axes(projection=’3d’)

ax.plot_surface(x_grid, y_grid, z, cmap=plt.cm.viridis)

“`

By changing the linspace parameters from np.arange() to np.linspace(), we create a denser grid for better aesthetics.

We also changed the figsize parameter to create a bigger figure for better visualization.

## Conclusion

In this article, we covered how to create 3D line and surface plots in Python using Matplotlib and NumPy libraries. We saw how to use the plot3D() function to plot a line in 3D space, and how to create a 3D surface plot using a grid of x and y values and a function to calculate Z values.

We also saw how to control the number of grid points for better aesthetics. These plots are valuable tools in visualizing complex data in fields such as engineering, physics, geography, and many more.

## Creating a 3D Wireframe Plot

In addition to 3D line and surface plots, another useful type of plot to display a continuous function is a 3D wireframe plot. This type of plot displays the function as a wireframe mesh, providing a more detailed look at the function.

In this section, we will learn how to create a 3D wireframe plot using the plot_wireframe() function in Matplotlib.

## Using plot_wireframe() function to plot a wireframe

The plot_wireframe() function is used to create a wireframe plot in 3D space. It takes three parameters: X, Y, and Z, where X and Y are 2D arrays that define the coordinates of the grid, and Z is a 2D array that defines the height of the wireframe at each point in the grid.

“`python

x_values = np.arange(-5, 5, 0.25)

y_values = np.arange(-5, 5, 0.25)

x_grid, y_grid = np.meshgrid(x_values, y_values)

z_grid = np.sin(np.sqrt(x_grid ** 2 + y_grid ** 2))

ax.plot_wireframe(x_grid, y_grid, z_grid)

“`

This code creates a wireframe plot of the function z = sin(sqrt(x^2 + y^2)).

## Changing the aesthetics by changing the grid size

Like surface plots, the number of grid points used to generate the wireframe can affect its aesthetics. Specifically, a denser grid will lead to a smoother looking wireframe.

Here’s an example of how we can adjust the density of the grid to create a better-looking wireframe:

“`python

x_values = np.arange(-5, 5, 0.1)

y_values = np.arange(-5, 5, 0.1)

x_grid, y_grid = np.meshgrid(x_values, y_values)

z_grid = np.sin(np.sqrt(x_grid ** 2 + y_grid ** 2))

fig = plt.figure(figsize=(8,8))

ax = plt.axes(projection=’3d’)

ax.plot_wireframe(x_grid, y_grid, z_grid)

“`

By adjusting the arange parameters to np.arange(-5, 5, 0.1), we create a denser grid to plot a smoother wireframe. We also changed the figsize parameter to create a bigger figure for better visualization.

## Interacting with the figure using mouse and drag

One of the benefits of 3D plots is the ability to interact with them in real-time using your mouse and drag. In our 3D wireframe plot, we can rotate, zoom, and pan the plot using our cursor.

## Here are some common mouse and drag interactions in 3D plots:

– To rotate the plot, click and drag your mouse on any part of the plot. – To zoom in and out of the plot, scroll your mouse wheel up and down.

– To pan the plot, hold the shift key and click and drag your mouse. Example code:

“`python

fig = plt.figure(figsize=(8, 8))

ax = plt.axes(projection=’3d’)

ax.plot_wireframe(x_grid, y_grid, z_grid)

plt.show()

“`

After executing this code, you should see the figure window pop up.

Click and drag anywhere on the figure to rotate the plot. Scroll your mouse wheel up and down to zoom in and out of the plot.

Hold the shift key and click and drag your mouse to pan the plot.

## Conclusion

In this article, we learned how to create a wireframe plot using the plot_wireframe() function in Matplotlib. We saw how to adjust the aesthetics of our wireframe plot by changing the density of the grid.

Finally, we explored how to interact with our 3D plot using our mouse and drag. These plots serve as powerful tools to visualize complex functions in a wide range of fields such as physics, finance, business, and many others.

In this article, we explored 3D plotting using Seaborn and Matplotlib libraries. We learned how to create 3D scatter plots, 3D line plots, 3D surface plots, and 3D wireframe plots.

We also covered how to adjust the aesthetics of each plot type to create a smoother, more detailed visualization of our functions. Finally, we learned how to interact with our 3D plot using our mouse and drag to rotate, pan, and zoom, providing a real-time experience.

By mastering 3D plotting, researchers, analysts, and data scientists can visualize and communicate complex relationships between variables in a more intuitive and digestible way. The ability to visualize functions and data in 3D opens doors to a more comprehensive and holistic understanding of the data and its nuances.