CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-scikit-spatial

Spatial objects and computations based on NumPy arrays for 2D, 3D, and higher-dimensional geometric calculations.

Pending
Overview
Eval results
Files

transforms-plotting.mddocs/

Transformations and Plotting

Coordinate system transformations and comprehensive matplotlib-based visualization support for both 2D and 3D plotting of spatial objects.

Capabilities

Coordinate Transformations

Transform points between different coordinate systems using custom origins and basis vectors.

def transform_coordinates(points, point_origin, vectors_basis) -> np.ndarray:
    """
    Transform points into a new coordinate system.
    
    Parameters:
    - points: array-like, (N, D) array of N points with dimension D
    - point_origin: array-like, (D,) origin of the new coordinate system
    - vectors_basis: sequence, basis vectors of the new coordinate system
                     Sequence of N_bases vectors, each with D elements
    
    Returns:
    (N, N_bases) array of coordinates in the new coordinate system
    
    Examples:
    Transform 2D points using standard and diagonal basis:
    >>> points = [[1, 2], [3, 4], [5, 6]]
    >>> vectors_basis = [[1, 0], [1, 1]]
    >>> transform_coordinates(points, [0, 0], vectors_basis)
    array([[ 1,  3],
           [ 3,  7], 
           [ 5, 11]])
    
    Transform 3D points to 2D coordinate system:
    >>> points = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    >>> vectors_basis = [[1, 0, 0], [-1, 1, 0]]
    >>> transform_coordinates(points, [0, 0, 0], vectors_basis)
    array([[1, 1],
           [4, 1],
           [7, 1]])
    """

Usage Example:

from skspatial.transformation import transform_coordinates
import numpy as np

# Transform 2D points to rotated coordinate system
points = [[1, 0], [0, 1], [1, 1]]
angle = np.pi / 4  # 45 degrees
rotation_basis = [
    [np.cos(angle), np.sin(angle)],    # Rotated x-axis
    [-np.sin(angle), np.cos(angle)]    # Rotated y-axis
]
transformed = transform_coordinates(points, [0, 0], rotation_basis)

# Transform 3D points to plane coordinate system
points_3d = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
plane_origin = [0, 0, 0]
plane_basis = [
    [1, 0, 0],  # x-direction in plane
    [0, 1, 0]   # y-direction in plane
]
plane_coords = transform_coordinates(points_3d, plane_origin, plane_basis)

2D and 3D Plotting

Comprehensive matplotlib-based visualization supporting all spatial objects with customizable styling and multiple object plotting.

def plot_2d(*plotters) -> Tuple:
    """
    Plot multiple spatial objects in 2D.
    
    Parameters:
    - *plotters: sequence of plotter functions from spatial objects
    
    Returns:
    Tuple (fig, ax) containing matplotlib Figure and Axes objects
    
    Examples:
    Plot multiple objects together:
    >>> from skspatial.objects import Point, Line
    >>> from skspatial.plotting import plot_2d
    >>> point = Point([1, 2])
    >>> line = Line([0, 0], [1, 1])
    >>> fig, ax = plot_2d(
    ...     point.plotter(color='red', s=100),
    ...     line.plotter(t_2=3, color='blue')
    ... )
    """

def plot_3d(*plotters) -> Tuple:
    """
    Plot multiple spatial objects in 3D.
    
    Parameters:
    - *plotters: sequence of plotter functions from spatial objects
    
    Returns:
    Tuple (fig, ax) containing matplotlib Figure and Axes3D objects
    
    Examples:
    Plot 3D objects together:
    >>> from skspatial.objects import Point, Plane, Sphere
    >>> from skspatial.plotting import plot_3d
    >>> point = Point([1, 2, 3])
    >>> plane = Plane([0, 0, 0], [0, 0, 1])
    >>> sphere = Sphere([0, 0, 0], 2)
    >>> fig, ax = plot_3d(
    ...     point.plotter(color='red', s=100),
    ...     plane.plotter(alpha=0.3),
    ...     sphere.plotter(alpha=0.5, color='blue')
    ... )
    """

Spatial Object Plotting Methods

All spatial objects provide plotting capabilities through consistent interfaces for 2D and 3D visualization.

# Common plotting interface for all spatial objects
class SpatialObject:
    def plotter(self, **kwargs):
        """
        Return plotting function for matplotlib integration.
        
        Parameters:
        - **kwargs: styling keywords passed to matplotlib functions
        
        Returns:
        Plotting function that can be used with plot_2d/plot_3d
        """
    
    def plot_2d(self, ax_2d, **kwargs):
        """
        Plot object directly on 2D matplotlib axes.
        
        Parameters:
        - ax_2d: matplotlib Axes, 2D plotting axes
        - **kwargs: styling keywords passed to matplotlib functions
        """
    
    def plot_3d(self, ax_3d, **kwargs):
        """
        Plot object directly on 3D matplotlib axes.
        
        Parameters:
        - ax_3d: matplotlib Axes3D, 3D plotting axes
        - **kwargs: styling keywords passed to matplotlib functions
        """

Object-Specific Plotting Parameters

Different spatial objects support specialized plotting parameters:

# Point plotting
Point.plot_2d(ax_2d, s=50, c='red', alpha=0.7, **kwargs)
Point.plot_3d(ax_3d, s=50, c='red', alpha=0.7, **kwargs)

# Vector plotting (with origin and scaling)
Vector.plot_2d(ax_2d, point=(0, 0), scalar=1, color='blue', **kwargs)
Vector.plot_3d(ax_3d, point=(0, 0, 0), scalar=1, color='blue', **kwargs)

# Line plotting (with parameter range)
Line.plot_2d(ax_2d, t_1=0, t_2=1, color='green', linewidth=2, **kwargs)
Line.plot_3d(ax_3d, t_1=0, t_2=1, color='green', linewidth=2, **kwargs)

# Plane plotting (with limits)
Plane.plot_3d(ax_3d, lims_x=(-5, 5), lims_y=(-5, 5), alpha=0.3, **kwargs)

# Circle plotting
Circle.plot_2d(ax_2d, fill=False, edgecolor='red', linewidth=2, **kwargs)

# Sphere plotting (with discretization)
Sphere.plot_3d(ax_3d, n_angles=30, alpha=0.5, color='blue', **kwargs)

# Cylinder plotting (with discretization)
Cylinder.plot_3d(ax_3d, n_along_axis=100, n_angles=30, alpha=0.7, **kwargs)

# Triangle plotting (with part selection)
Triangle.plot_2d(ax_2d, part='points', s=50, c='red', **kwargs)
Triangle.plot_3d(ax_3d, part='edges', color='black', linewidth=1, **kwargs)

Usage Examples:

from skspatial.objects import Point, Vector, Line, Plane, Circle, Sphere
from skspatial.plotting import plot_2d, plot_3d
import matplotlib.pyplot as plt

# 2D plotting example
point = Point([1, 2])
vector = Vector([2, 1])
line = Line([0, 0], [1, 1])
circle = Circle([0, 0], 3)

fig, ax = plot_2d(
    point.plotter(s=100, c='red', label='Point'),
    vector.plotter(point=[1, 2], scalar=1.5, color='blue', label='Vector'),
    line.plotter(t_1=-2, t_2=4, color='green', label='Line'),
    circle.plotter(fill=False, edgecolor='purple', linewidth=2, label='Circle')
)
ax.set_aspect('equal')
ax.legend()
ax.grid(True)
plt.show()

# 3D plotting example
point_3d = Point([1, 2, 3])
plane = Plane([0, 0, 0], [0, 0, 1])
sphere = Sphere([2, 2, 2], 1.5)
line_3d = Line([0, 0, 0], [1, 1, 1])

fig, ax = plot_3d(
    point_3d.plotter(s=100, c='red', label='Point'),
    plane.plotter(lims_x=(-3, 3), lims_y=(-3, 3), alpha=0.3, color='gray'),
    sphere.plotter(alpha=0.6, color='blue', label='Sphere'),
    line_3d.plotter(t_1=0, t_2=3, color='green', linewidth=3, label='Line')
)
ax.set_xlabel('X')
ax.set_ylabel('Y') 
ax.set_zlabel('Z')
ax.legend()
plt.show()

# Direct plotting on existing axes
fig = plt.figure(figsize=(12, 5))

# 2D subplot
ax1 = fig.add_subplot(1, 2, 1)
point.plot_2d(ax1, s=100, c='red')
circle.plot_2d(ax1, fill=False, edgecolor='blue', linewidth=2)
ax1.set_aspect('equal')
ax1.grid(True)
ax1.set_title('2D Plot')

# 3D subplot  
ax2 = fig.add_subplot(1, 2, 2, projection='3d')
point_3d.plot_3d(ax2, s=100, c='red')
sphere.plot_3d(ax2, alpha=0.5, color='blue')
ax2.set_title('3D Plot')

plt.tight_layout()
plt.show()

# Animation example (plotting objects at different times)
import numpy as np

fig, ax = plt.subplots()

# Animate a point moving along a line
line = Line([0, 0], [1, 1])
t_values = np.linspace(0, 5, 50)

for t in t_values:
    ax.clear()
    
    # Plot the line
    line.plot_2d(ax, t_1=0, t_2=5, color='blue', alpha=0.5)
    
    # Plot point at current position
    current_point = line.to_point(t)
    current_point.plot_2d(ax, s=100, c='red')
    
    ax.set_xlim(-1, 6)
    ax.set_ylim(-1, 6)
    ax.set_aspect('equal')
    ax.grid(True)
    ax.set_title(f'Point at t={t:.1f}')
    
    plt.pause(0.1)

plt.show()

Integration with NumPy and Scientific Python

The plotting system integrates seamlessly with NumPy arrays and other scientific Python libraries:

import numpy as np
from skspatial.objects import Points, Line, Plane
from skspatial.plotting import plot_3d

# Generate random point cloud
np.random.seed(42)
points_array = np.random.randn(100, 3)
points = Points(points_array)

# Fit geometric objects to data
line_fit = Line.best_fit(points_array)
plane_fit = Plane.best_fit(points_array)

# Visualize data and fitted objects
fig, ax = plot_3d(
    points.plotter(s=30, c='gray', alpha=0.6, label='Data'),
    line_fit.plotter(t_1=-3, t_2=3, color='red', linewidth=3, label='Best Fit Line'),
    plane_fit.plotter(lims_x=(-3, 3), lims_y=(-3, 3), alpha=0.3, color='blue', label='Best Fit Plane')
)
ax.legend()
ax.set_title('Geometric Fitting Visualization')
plt.show()

Install with Tessl CLI

npx tessl i tessl/pypi-scikit-spatial

docs

basic-objects.md

curved-geometry.md

index.md

linear-geometry.md

measurement.md

transforms-plotting.md

tile.json