wmaee.scopes.md
Analysis and plotting of (AI)MD runs
1"""Analysis and plotting of (AI)MD runs""" 2 3import numpy as np 4import pandas as pd 5import matplotlib.pyplot as plt 6from typing import Optional, Tuple, List 7 8def plot_MD(df: pd.DataFrame, 9 time: Optional[str] = None, 10 timestep: int = 1, 11 grid: Tuple[int, int] = (1, 1), 12 props: List[str] = [], 13 labels: Optional[List[str]] = None, 14 show: bool = True, 15 **kwargs) -> Tuple[plt.Figure, np.ndarray]: 16 """ 17 Plots multiple properties from a DataFrame in a grid layout. 18 19 Parameters 20 ---------- 21 df : pd.DataFrame 22 The DataFrame containing the data to plot. 23 time : Optional[str], optional 24 The column name to use as the time axis. If None, a default range is used, by default None. 25 timestep : int, optional 26 The timestep interval for the default time axis, by default 1. 27 grid : Tuple[int, int], optional 28 The grid size for the subplots (rows, columns), by default (1, 1). 29 props : List[str], optional 30 The list of DataFrame columns to plot, by default []. 31 labels : Optional[List[str]], optional 32 The labels for the y-axis of each subplot, by default None. 33 show : bool, optional 34 Whether to display the plot, by default True. 35 **kwargs 36 Additional keyword arguments passed to `plt.subplots`. 37 38 Returns 39 ------- 40 Tuple[plt.Figure, np.ndarray] 41 The figure and axes array of the created subplots. 42 """ 43 44 # Determine the time axis values 45 if time is None: 46 time = np.arange(len(df)) * timestep 47 else: 48 time = df[time] 49 50 # Create subplots with the specified grid size 51 fig, axs = plt.subplots(*grid, squeeze=False, sharex=True, **kwargs) 52 53 # Iterate through the grid and plot the data 54 for i in range(grid[0]): 55 for j in range(grid[1]): 56 n = i * grid[1] + j 57 if n >= len(props): 58 break 59 # Plot the data for the nth property 60 axs[i, j].plot(time, df[props[n]]) 61 # Set the y-axis label if provided 62 if labels is not None: 63 axs[i, j].set_ylabel(labels[n]) 64 # Set the x-axis label for the last row 65 axs[-1, j].set_xlabel('step') 66 else: 67 continue 68 break 69 70 # Adjust subplot spacing 71 fig.subplots_adjust(hspace=0) 72 # Show the plot if requested 73 if show: 74 plt.show() 75 76 return fig, axs
def
plot_MD( df: pandas.core.frame.DataFrame, time: Optional[str] = None, timestep: int = 1, grid: Tuple[int, int] = (1, 1), props: List[str] = [], labels: Optional[List[str]] = None, show: bool = True, **kwargs) -> Tuple[matplotlib.figure.Figure, numpy.ndarray]:
9def plot_MD(df: pd.DataFrame, 10 time: Optional[str] = None, 11 timestep: int = 1, 12 grid: Tuple[int, int] = (1, 1), 13 props: List[str] = [], 14 labels: Optional[List[str]] = None, 15 show: bool = True, 16 **kwargs) -> Tuple[plt.Figure, np.ndarray]: 17 """ 18 Plots multiple properties from a DataFrame in a grid layout. 19 20 Parameters 21 ---------- 22 df : pd.DataFrame 23 The DataFrame containing the data to plot. 24 time : Optional[str], optional 25 The column name to use as the time axis. If None, a default range is used, by default None. 26 timestep : int, optional 27 The timestep interval for the default time axis, by default 1. 28 grid : Tuple[int, int], optional 29 The grid size for the subplots (rows, columns), by default (1, 1). 30 props : List[str], optional 31 The list of DataFrame columns to plot, by default []. 32 labels : Optional[List[str]], optional 33 The labels for the y-axis of each subplot, by default None. 34 show : bool, optional 35 Whether to display the plot, by default True. 36 **kwargs 37 Additional keyword arguments passed to `plt.subplots`. 38 39 Returns 40 ------- 41 Tuple[plt.Figure, np.ndarray] 42 The figure and axes array of the created subplots. 43 """ 44 45 # Determine the time axis values 46 if time is None: 47 time = np.arange(len(df)) * timestep 48 else: 49 time = df[time] 50 51 # Create subplots with the specified grid size 52 fig, axs = plt.subplots(*grid, squeeze=False, sharex=True, **kwargs) 53 54 # Iterate through the grid and plot the data 55 for i in range(grid[0]): 56 for j in range(grid[1]): 57 n = i * grid[1] + j 58 if n >= len(props): 59 break 60 # Plot the data for the nth property 61 axs[i, j].plot(time, df[props[n]]) 62 # Set the y-axis label if provided 63 if labels is not None: 64 axs[i, j].set_ylabel(labels[n]) 65 # Set the x-axis label for the last row 66 axs[-1, j].set_xlabel('step') 67 else: 68 continue 69 break 70 71 # Adjust subplot spacing 72 fig.subplots_adjust(hspace=0) 73 # Show the plot if requested 74 if show: 75 plt.show() 76 77 return fig, axs
Plots multiple properties from a DataFrame in a grid layout.
Parameters
- df (pd.DataFrame): The DataFrame containing the data to plot.
- time (Optional[str], optional): The column name to use as the time axis. If None, a default range is used, by default None.
- timestep (int, optional): The timestep interval for the default time axis, by default 1.
- grid (Tuple[int, int], optional): The grid size for the subplots (rows, columns), by default (1, 1).
- props (List[str], optional): The list of DataFrame columns to plot, by default [].
- labels (Optional[List[str]], optional): The labels for the y-axis of each subplot, by default None.
- show (bool, optional): Whether to display the plot, by default True.
- **kwargs: Additional keyword arguments passed to
plt.subplots.
Returns
- Tuple[plt.Figure, np.ndarray]: The figure and axes array of the created subplots.