矢量场可视化需求:基于6列数据实现带切面的3D矢量与颜色映射图
Got it, let's break down how to add interactive cut planes to your Mayavi 3D vector field visualization—whether you want to show vectors on the plane or use colormaps to highlight field properties like magnitude or component values. Here's a step-by-step solution with code examples you can adapt to your data:
1. Show Vectors on an Interactive Cut Plane
This approach lets you drag and rotate a plane through your 3D field, and automatically displays the vectors that lie on that plane (highlighted in a distinct color for clarity).
Code Implementation
import numpy as np from mayavi import mlab # ---------------------- # Replace this with your actual data arrays: x, y, z, px, py, pz # ---------------------- # Example structured grid data (simulate your field) x, y, z = np.mgrid[-2:2:20j, -2:2:20j, -2:2:20j] px = -y # Sample x-component py = x # Sample y-component pz = np.sin(z) # Sample z-component # Set up the main figure mlab.figure(size=(800, 600), bgcolor=(0.1, 0.1, 0.1)) # Draw the full 3D vector field (faded color to keep focus on the plane) full_quiver = mlab.quiver3d(x, y, z, px, py, pz, scale_factor=0.3, color=(0.8, 0.8, 0.8)) # Create an interactive plane widget to define the cut plane plane_widget = mlab.pipeline.plane_widget(full_quiver, plane_orientation='z_axes', center=(0, 0, 0)) # Define a callback function to update the plane's vectors when the widget is moved def update_plane_vectors(field): # Get the plane's origin and normal vector from the widget plane = field.widget origin = plane.origin normal = plane.normal # Calculate distance from each point to the plane; filter points close to the plane distances = np.abs((x - origin[0])*normal[0] + (y - origin[1])*normal[1] + (z - origin[2])*normal[2]) plane_mask = distances < 0.05 # Adjust threshold based on your grid density # Extract points and vectors on the plane x_cut, y_cut, z_cut = x[plane_mask], y[plane_mask], z[plane_mask] px_cut, py_cut, pz_cut = px[plane_mask], py[plane_mask], pz[plane_mask] # Clear and redraw to update the visualization mlab.clf(mlab.gcf()) mlab.quiver3d(x, y, z, px, py, pz, scale_factor=0.3, color=(0.8, 0.8, 0.8)) # Highlight plane vectors with a bright color (red here) mlab.quiver3d(x_cut, y_cut, z_cut, px_cut, py_cut, pz_cut, scale_factor=0.3, color=(1, 0, 0)) mlab.draw() # Link the callback to the plane widget's interaction events plane_widget.on_trait_change(update_plane_vectors, 'widget') mlab.show()
Key Notes
- Adjust the
distances < 0.05threshold to match your data's grid spacing—smaller values mean stricter filtering of points on the plane. - Change the
colorparameter for the plane vectors to whatever works best for your visualization.
2. Show Colormapped Scalar Data on a Cut Plane
If you want to visualize scalar properties of your vector field (like magnitude, or individual components) on a cut plane, you can use Mayavi's scalar_cut_plane tool. This is great for spotting gradients or regions of high/low field strength.
Code Implementation
import numpy as np from mayavi import mlab # ---------------------- # Replace with your actual data # ---------------------- x, y, z = np.mgrid[-2:2:20j, -2:2:20j, -2:2:20j] px = -y py = x pz = np.sin(z) # Calculate a scalar property to visualize (e.g., vector magnitude) vector_magnitude = np.sqrt(px**2 + py**2 + pz**2) # Alternatively, use a single component: scalar_data = px # Set up the figure and full vector field mlab.figure(size=(800, 600), bgcolor=(0.1, 0.1, 0.1)) mlab.quiver3d(x, y, z, px, py, pz, scale_factor=0.3, color=(0.8, 0.8, 0.8)) # Create a scalar field from your chosen property scalar_field = mlab.pipeline.scalar_field(x, y, z, vector_magnitude) # Add an interactive cut plane with colormap cut_plane = mlab.pipeline.scalar_cut_plane( scalar_field, plane_orientation='z_axes', # Start with a z-aligned plane colormap='viridis', # Choose a colormap (try 'coolwarm' or 'plasma' too) opacity=0.7, # Make the plane semi-transparent to see vectors behind it enable_contours=True # Optional: add contour lines for better contrast ) cut_plane.contour.number_of_contours = 5 # Adjust number of contour lines mlab.show()
Key Notes
- Swap
vector_magnitudewithpx,py, orpzto visualize individual vector components on the plane. - Adjust
opacityto balance visibility of the plane and the underlying 3D vectors. - Explore Mayavi's built-in colormaps to find one that best highlights your field's features.
Both methods work with structured or unstructured grid data—just make sure your x, y, z, px, py, pz are numpy arrays of the same shape.
内容的提问来源于stack exchange,提问作者Franco Di Rino

