Lecture 3 - 3D Plots Overview
3D Data Visualization in Python
Overview
Course: CS-657: Scientific Data Visualization
Instructor: Dr. Rohit P. Singh
Institution: University of Wisconsin, Milwaukee
Date: January 29, 2025
Introduction to 3D Visualization
3D visualization is a critical aspect of scientific data analysis that allows researchers to represent complex data structures visually. In Python, several libraries facilitate 3D plotting and visualization, including:
mpl_toolkits: A toolkit formatplotlibproviding 3D plotting capabilities, essential for crafting detailed visual representations of data.numpy: A fundamental package for numerical computations in Python, useful for handling large datasets and performing array operations efficiently.matplotlib.pyplot: The primary plotting library in Python, providing a wide array of plotting functionalities including support for 3D plots.
Basic 3D Projection
Code Snippet:
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection ='3d')
plt.show()
This code snippet creates a simple 3D projection window that sets the stage for further 3D visualizations. It initializes a new figure and an axes object from the mplot3d module, providing a blank canvas for rendering 3D graphics.
3D Scatter Plots
To create a 3D scatter plot, first generate z-coordinates using:
z = np.linspace(0, 1, 100)to establish depth.For calculating x and y coordinates, use sinusoidal functions based on the z-coordinates:
x = z * np.sin(25 * z)y = z * np.cos(25 * z)
Code to Create a 3D Scatter Plot:
ax.scatter3D(x, y, z, color='green')
ax.set_title('3D Scatter Plot')
plt.savefig("3D-projection1.png", bbox_inches='tight', pad_inches=0)
plt.show()
This code generates a scatter plot with green points, illustrating the relationship among the variables in three dimensions visually.
3D Line Plots
To create 3D line plots, you can use a mathematical function to define your data:
return np.sin(np.sqrt(x ** 2 + y ** 2)), which generates a smooth curve based on the calculated distances in three-dimensional space.Code Snippet for a 3D Line Plot:
ax.plot3D(x, y, z)
ax.set_title('3D Line Plot')
plt.savefig("3D-projection4.png", bbox_inches='tight', pad_inches=0)
plt.show()
The provided code draws a continuous line connecting the data points, effectively visualizing the underlying mathematical function in 3D.
Scatter and Line Combined
This technique demonstrates the combination of scatter and line plots within a single graph, providing a comprehensive view of both individual data points and their connections or trends over time. This integration is particularly useful in observing correlations in multi-dimensional datasets.
Surface Plots
To create surface plots, define x and y ranges and evaluate a function over them. Utilize meshgrid to create a grid for x and y that feeds into the function to compute z values.
Code for Surface Plot:
ax.plot_surface(X, Y, Z, cmap='viridis')
ax.set_title('3D Surface Plot')
plt.savefig("3D-projection2.png", bbox_inches='tight', pad_inches=0)
plt.show()
This code generates a surface plot based on the evaluated z-coordinates, visually representing the data's surface through colors defined by the viridis colormap, which offers a gradient visualization.
Wire-Frame Plots
Wire-frame plots provide a skeletal representation of 3D surfaces, displaying only the outlines of the structure. This format allows for a clear understanding of the surface shape without the distraction of filled colors, making them useful for structural analysis of data points.
3D Points and Lines
You can create multiple lines in 3D, defined by start and end points:
Variables:
VecStart_x, VecStart_y, VecStart_zVecEnd_x, VecEnd_y, VecEnd_z
Example Code:
for i in range(4):
ax.plot([VecStart_x[i], VecEnd_x[i]], [VecStart_y[i], VecEnd_y[i]], zs=[VecStart_z[i], VecEnd_z[i]])
This code snippet illustrates how to plot multiple lines in 3D space, enabling detailed path visualization among various defined points.
3D Triangles
Plots triangles in 3D space based on vertices defined by X, Y, and Z coordinates. This is particularly useful in generating 3D meshes or approximating complex surfaces with triangular elements.
Example Code for Triangle Plot:
ax.plot_trisurf(X, Y, Z, color='cyan')
Here, triangles created from the specified vertices form a surface, allowing for detailed surface analysis of data.
3D Plot Rotation
You can adjust the perspective of a 3D plot using azimuthal and elevation angles with the command: ax.view_init(-140, 60), which modifies how the plot is viewed in a 3-dimensional space, crucial for examining the entire structure from different angles for a comprehensive analysis.
Free Triangle Representation
Creating a custom triangle shape in 3D space showcases the capability of rendering polygons using distinctive face colors. This is useful for visualizing geometric shapes and complex data forms, enhancing interpretability in scientific visualizations.
Saving 3D Plots
To save a 3D plot in different formats, you can utilize the following command:
plt.savefig("3D-projection2.pdf")
This command allows for exporting high-quality visualizations for presentations or publications, ensuring that the visual representation remains intact across different media formats.
Conclusion
3D visualization in Python empowers users to represent diverse scientific data visually, including scatter plots, line plots, surface plots, and more. These tools are vital for data analysis, enabling clearer insights and better understanding of complex datasets in research and applications.