openpytea.plotting¶
- openpytea.plotting.plot_stacked_bar(data, figsize=(1.2, 1.8), ax=None, show=True)[source]¶
Create a stacked bar chart with sorted components. This function generates a stacked bar chart where components are sorted by their total values across all bars in descending order. Components are color-coded and displayed with a legend.
As a special case (used by
openpytea.analysis.levelized_cost_data()), a component named “Side revenue” (expected to hold a negative value, since it is subtracted from the total) forms the base of the stack below zero: the remaining components stack on top of it, so the top of each bar reads as the correct net total (e.g., the LCOP). It is drawn last (on top, z-order-wise) so it stays visible where it overlaps the other components, and reuses the color of the largest stacked component (distinguished only by a hatch pattern) so the CAPEX/OPEX color ratio remains easy to read. :param data: Dictionary containing the following keys:- “components”list of dict
List of dictionaries (one per bar) mapping component names to their values.
- “xlabels”list
Labels for the x-axis (one per bar).
- “currency”str
Currency symbol or unit to display in y-axis label (ignored if pct=True).
- “pct”bool
If True, values are interpreted as percentages and displayed accordingly.
- “ylabel”str
Label for the y-axis.
- Parameters:
figsize (
tupleoffloat, optional) – Figure size as (width, height) in inches. Default is (1.2, 1.8). The width is automatically adjusted based on the number of bars.ax (
matplotlib.axes.Axes, optional) – Existing axes object to plot on. If None, a new figure and axes are created. Default is None.show (
bool, optional) – If True and a new figure is created, display the plot. Default is True.
- Returns:
The figure and axes containing the stacked bar chart. When ax is supplied by the caller,
figisax.figure.- Return type:
tupleof(matplotlib.figure.Figure,matplotlib.axes.Axes)
Notes
- Components are sorted in descending order by their total value across
all bars.
Bar spacing is set to 0.75 and bar width is 0.45.
- Colors are automatically assigned from a colormap and consistent across
bars.
The legend is positioned to the right of the plot area.
- When pct=True and n_bars=1, the percentage value is appended to the
component label.
- When pct=True, percentages are relative to the sum of the stacked
(non-side-revenue) components in each bar.
- openpytea.plotting.plot_sensitivity(data, figsize=(3.2, 2.2), ax=None, show=True)[source]¶
Plot sensitivity analysis curves. :param data: Dictionary containing plot data with the following keys:
- ‘curves’list of dict
List of curve dictionaries, each containing: - ‘x’ : array-like
X-axis values for the curve
- ‘y’array-like
Y-axis values for the curve
- ‘plant’str
Label/name for the curve legend
- ‘xlabel’str
Label for the x-axis
- ‘ylabel’str
Label for the y-axis
- Parameters:
figsize (
tuple, optional) – Figure size as (width, height) in inches. Default is (3.2, 2.2).ax (
matplotlib.axes.Axes, optional) – Axes object to plot on. If None, a new figure and axes are created. Default is None.show (
bool, optional) – Whether to display the plot. Default is True.
- Returns:
The figure and axes containing the plotted sensitivity curves. When ax is supplied by the caller,
figisax.figure.- Return type:
tupleof(matplotlib.figure.Figure,matplotlib.axes.Axes)
Notes
Multiple curves are plotted with colors from the Set2 colormap.
If a new figure is created, tight_layout() is applied before showing.
- If an existing axes is provided, canvas is redrawn instead of calling
plt.show().
- openpytea.plotting.plot_tornado(data, figsize=(3.4, 2.4), ax=None, show=True)[source]¶
Create a tornado plot visualization for sensitivity analysis. A tornado plot displays the impact of parameter variations on a base value, showing positive and negative deviations as horizontal bars. :param data: Dictionary containing the following keys:
- ‘lows’array-like
Lower bound values for each parameter.
- ‘highs’array-like
Upper bound values for each parameter.
- ‘base_value’float
Reference/baseline value for comparison.
- ‘labels’list of str
Parameter names for y-axis labels.
- ‘xlabel’str, optional
Label for x-axis. If None, no label is set.
- ‘plus_minus_value’float, optional
Percentage variation value displayed in legend (e.g., 0.1 for 10%).
- Parameters:
figsize (
tuple, optional) – Figure size as (width, height) in inches. Default is (3.4, 2.4).ax (
matplotlib.axes.Axes, optional) – Existing axes object to plot on. If None, a new figure and axes are created. Default is None.show (
bool, optional) – If True and a new figure is created, display the plot using plt.show(). Default is True.
- Returns:
The figure and axes containing the tornado plot. When ax is supplied by the caller,
figisax.figure.- Return type:
tupleof(matplotlib.figure.Figure,matplotlib.axes.Axes)
Notes
Blue bars represent negative deviations (lows from base_value).
Red bars represent positive deviations (highs from base_value).
A dashed black line indicates the base_value on the x-axis.
Automatic padding is applied to x-axis limits (5% of span or range).
- openpytea.plotting.plot_cash_flow(data, figsize=(3.6, 2.6), ax=None, show=True)[source]¶
Plot a project cash flow diagram (cumulative cash position vs. time).
Draws the classic cumulative cash flow curve used to visualize capital recovery over a project’s life: the curve dips into debt during construction/start-up as CAPEX and working capital are spent, bottoms out at the “maximum investment”, crosses back above zero at the break-even point, and then climbs as operating profit accumulates.
- Parameters:
data (
dict) – Dictionary as returned byopenpytea.analysis.cash_flow_data(), containing a “curves” list (one entry per plant with “years”, “cumulative”, “breakeven_year”, “max_investment”, and “max_investment_year”), plus “xlabel”, “ylabel”, and “currency”.figsize (
tuple, optional) – Figure size as (width, height) in inches. Default is (3.6, 2.6).ax (
matplotlib.axes.Axes, optional) – Existing axes object to plot on. If None, a new figure and axes are created. Default is None.show (
bool, optional) – If True and a new figure is created, display the plot using plt.show(). Default is True.
- Returns:
The figure and axes containing the cash flow diagram. When ax is supplied by the caller,
figisax.figure.- Return type:
tupleof(matplotlib.figure.Figure,matplotlib.axes.Axes)
Notes
For every plant, the region where its cumulative cash flow is negative is shaded (hatched) as debt, and its break-even point (if any) is marked with a dashed vertical line in the same color as its curve.
- openpytea.plotting.plot_monte_carlo(data, metric=None, bins=30, show_fit=True, label=None, figsize=(3.2, 2.2), ax=None, show=True)[source]¶
Plot a histogram with a fitted normal distribution overlay for Monte Carlo simulation results. This function visualizes Monte Carlo data by displaying a histogram of sample values overlaid with a normal distribution curve fitted to the data. It supports multiple input types: Monte Carlo data dictionaries, Plant objects with monte_carlo_metrics, or raw numpy arrays. :param data: The Monte Carlo data to plot. Can be:
A dictionary containing a “metrics” key with metric values
A Plant object with a monte_carlo_metrics attribute
A list or tuple of Plant objects
A raw array-like of numeric values
- Parameters:
metric (
str, optional) – The name of the metric to plot (case-insensitive). Default is “LCOP”. Must be present in the data’s available metrics.bins (
int, optional) – Number of histogram bins. Default is 30.show_fit (
bool, optional) – Whether to overlay a normal distribution fitted to the data (or, when the fitted standard deviation is 0, a vertical line at the mean). If False, only the histogram is plotted and the legend is omitted. Default is True.label (
strorNone, optional) – X-axis label. If None, a default label is generated based on the metric and currency. Default is None.figsize (
tuple, optional) – Figure size as (width, height) in inches. Default is (3.2, 2.2).ax (
matplotlib.axes.Axes, optional) – Existing matplotlib axes to plot on. If None, a new figure and axes are created. Default is None.show (
bool, optional) – Whether to display the plot using plt.show(). Only applies if a new figure was created. Default is True.
- Returns:
The figure and axes containing the plot. When ax is supplied by the caller,
figisax.figure.- Return type:
tupleof(matplotlib.figure.Figure,matplotlib.axes.Axes)- Raises:
ValueError – If the specified metric is not found in the input data’s available metrics.
Notes
When
show_fitis True, the normal distribution parameters (μ, σ)
are fitted to the data using scipy.stats.norm.fit() - The standard deviation is formatted in scientific notation if |σ|>= 1000 or < 0.001 - The histogram is semi-transparent (alpha=0.6) with black edges for better visibility - Colors are cycled from matplotlib’s tab10 colormap for histogram and distribution curve
- openpytea.plotting.plot_monte_carlo_inputs(data, figsize=None, bins=50, show=True)[source]¶
Plot histograms of Monte Carlo input parameters. This function creates a grid of histograms visualizing the distribution of input parameters from a Monte Carlo simulation. Each parameter is displayed in its own subplot, arranged in a grid layout with 3 columns. :param data: Input data containing Monte Carlo parameters. Can be either:
A dictionary with an “inputs” key containing the parameters dict
A dictionary directly mapping parameter names to arrays of values
- Parameters:
inputs (
dict) – Dictionary where keys are parameter names (str) and values are array like objects containing the parameter samples.figsize (
tupleof(float,float), optional) – Figure size as (width, height) in inches. If None, automatically calculated based on number of parameters (default: None).bins (
int, optional) – Number of histogram bins for each parameter (default: 50).show (
bool, optional) – If True, displays the figure and calls tight_layout(). If False, only returns the axes without displaying (default: True).
- Returns:
The figure and an array of Axes objects (one per subplot). For a single parameter the array has length 1; for multiple parameters it is a flattened 1-D array of subplots.
- Return type:
tupleof(matplotlib.figure.Figure,numpy.ndarray)
Notes
Histograms are plotted with density normalization enabled
Unused subplots (when n_params is not a multiple of 3) are hidden
Each histogram is displayed with black edges and 70% transparency
- openpytea.plotting.plot_multiple_monte_carlo(data_list, metric='LCOP', bins=30, show_fit=True, figsize=None, label=None, ax=None, show=True)[source]¶
Plot multiple Monte Carlo simulation results as overlaid histograms with fitted normal distributions.
- Parameters:
data_list (
listofdictorlistofPlant) – List of Monte Carlo data dictionaries or Plant objects.metric (
str, optional) – Metric to plot from Monte Carlo results (default: “LCOP”).bins (
int, optional) – Number of histogram bins (default: 30).show_fit (
bool, optional) – Whether to overlay a normal distribution fitted to each dataset (or, when the fitted standard deviation is 0, a vertical line at the mean). If False, only the histograms are plotted. Default is True.figsize (
tuple, optional) – Figure size as (width, height).label (
str, optional) – Label for the x-axis.ax (
matplotlib.axes.Axes, optional) – Axes object to plot on.show (
bool, optional) – Whether to display the plot.
- Returns:
The figure and axes containing the overlaid histograms. When ax is supplied by the caller,
figisax.figure.- Return type:
tupleof(matplotlib.figure.Figure,matplotlib.axes.Axes)