Plotter¶
- class modusa.tools.plotter.Plotter[source]¶
Bases:
ModusaTool
Plots different kind of signals using matplotlib.
Note
The class has plot_ methods to plot different types of signals (1D, 2D).
- static plot_signal(y: ndarray, x: ndarray | None, ax: Axes | None = None, fmt: str = 'k', title: str | None = None, label: str | None = None, ylabel: str | None = None, xlabel: str | None = None, ylim: tuple[float, float] | None = None, xlim: tuple[float, float] | None = None, highlight: list[tuple[float, float], ...] | None = None, vlines: list[float] | None = None, hlines: list[float] | None = None, show_grid: bool = False, stem: bool = False, legend_loc: str = None) Figure | None [source]¶
Plots 1D signal using matplotlib with various settings passed through the arguments.
from modusa.io import Plotter import numpy as np # Generate a sample sine wave x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x) # Plot the signal fig = Plotter.plot_signal( y=y, x=x, ax=None, color="blue", marker=None, linestyle="-", stem=False, labels=("Time", "Amplitude", "Sine Wave"), legend_loc="upper right", zoom=None, highlight=[(2, 4)] )
- Parameters:
y (np.ndarray) – The signal values to plot on the y-axis.
x (np.ndarray | None) – The x-axis values. If None, indices of y are used.
ax (plt.Axes | None) – matplotlib Axes object to draw on. If None, a new figure and axis are created. Return type depends on parameter value.
color (str) – Color of the plotted line or markers. (e.g. “k”)
marker (str | None) – marker style for the plot (e.g., ‘o’, ‘x’). If None, no marker is used.
linestyle (str | None) – Line style for the plot (e.g., ‘-’, ‘–‘). If None, no line is drawn.
stem (bool) – If True, plots a stem plot.
labels (tuple[str, str, str] | None) – Tuple containing (title, xlabel, ylabel). If None, no labels are set.
legend_loc (str | None) – Location string for legend placement (e.g., ‘upper right’). If None, no legend is shown.
zoom (tuple | None) – Tuple specifying x-axis limits for zoom as (start, end). If None, full x-range is shown.
highlight (list[tuple[float, float], ...] | None) – List of (start, end) tuples to highlight regions on the plot. e.g. [(1, 2.5), (6, 10)]
- Returns:
Figure if ax is None else None.
- Return type:
plt.Figure | None
- static plot_matrix(M: ndarray, r: ndarray | None = None, c: ndarray | None = None, ax: Axes | None = None, cmap: str = 'gray_r', title: str | None = None, Mlabel: str | None = None, rlabel: str | None = None, clabel: str | None = None, rlim: tuple[float, float] | None = None, clim: tuple[float, float] | None = None, highlight: list[tuple[float, float, float, float]] | None = None, vlines: list[float] | None = None, hlines: list[float] | None = None, origin: str = 'lower', gamma: int | float | None = None, show_colorbar: bool = True, cax: Axes | None = None, show_grid: bool = True, tick_mode: str = 'center', n_ticks: tuple[int, int] | None = None) Figure [source]¶
Plot a 2D matrix with optional zooming, highlighting, and grid.
from modusa.io import Plotter import numpy as np import matplotlib.pyplot as plt # Create a 50x50 random matrix M = np.random.rand(50, 50) # Coordinate axes r = np.linspace(0, 1, M.shape[0]) c = np.linspace(0, 1, M.shape[1]) # Plot the matrix fig = Plotter.plot_matrix( M=M, r=r, c=c, log_compression_factor=None, ax=None, labels=None, zoom=None, highlight=None, cmap="viridis", origin="lower", show_colorbar=True, cax=None, show_grid=False, tick_mode="center", n_ticks=(5, 5), )
- Parameters:
M (np.ndarray) – 2D matrix to plot.
r (np.ndarray) – Row coordinate axes.
c (np.ndarray) – Column coordinate axes.
log_compression_factor (int | float | None) – Apply log compression to enhance contrast (if provided).
ax (plt.Axes | None) – Matplotlib axis to draw on (creates new if None).
labels (tuple[str, str, str, str] | None) – Labels for the plot (title, Mlabel, xlabel, ylabel).
zoom (tuple[float, float, float, float] | None) – Zoom to (r1, r2, c1, c2) in matrix coordinates.
highlight (list[tuple[float, float, float, float]] | None) – List of rectangles (r1, r2, c1, c2) to highlight.
cmap (str) – Colormap to use.
origin (str) – Image origin, e.g., “upper” or “lower”.
show_colorbar (bool) – Whether to display colorbar.
cax (plt.Axes | None) – Axis to draw colorbar on (ignored if show_colorbar is False).
show_grid (bool) – Whether to show grid lines.
tick_mode (str) – Tick alignment mode: “center” or “edge”.
n_ticks (tuple[int, int]) – Number of ticks on row and column axes.
- Returns:
Matplotlib figure containing the plot.
- Return type:
plt.Figure