plot_curve#

pymc_marketing.plot.plot_curve(curve, non_grid_names, n_samples=10, hdi_probs=None, random_seed=None, subplot_kwargs=None, sample_kwargs=None, hdi_kwargs=None, axes=None, same_axes=False, colors=None, legend=None, sel_to_string=None, color_dim=None)[source]#

Plot HDI with samples of the curve across coords.

Parameters:
  • curve (DataArray) – Curve to plot

  • non_grid_names (str | set[str]) – The names to exclude from the grid. HDI and samples both have defaults of hdi and chain, draw, respectively

  • n_samples (int) – Number of samples to plot

  • hdi_probs (float | list[float] | None) – HDI probabilities. Defaults to None which uses arviz default for stats.ci_prob which is 94%

  • random_seed (Generator | None) – Random number generator. Defaults to None which uses np.random.default_rng()

  • subplot_kwargs (dict | None) – Additional kwargs to while creating the fig and axes

  • sample_kwargs (dict | None) – Kwargs for the plot_samples() function

  • hdi_kwargs (dict | None) – Kwargs for the plot_hdi() function

  • same_axes (bool) – If all of the plots are on the same axis

  • colors (Iterable[str] | None) – Colors for the plots

  • legend (bool | None) – If to include a legend. Defaults to True if same_axes or color_dim

  • sel_to_string (Callable[[dict[str, Any]], str] | None) – Function to convert selection to a string. Defaults to “, “.join(f”{key}={value}” for key, value in sel.items())

  • color_dim (str | None) – Dimension to use for color grouping within each facet subplot. Values of this dimension are plotted with distinct colors and appear as legend entries, instead of being faceted into separate subplots. For example, color_dim="brand" facets by geo and colors by brand.

Return type:

tuple[plt.Figure, npt.NDArray[plt.Axes]]

Returns:

Figure and the axes

Examples

Plot prior for arbitrary Deterministic in PyMC model

import numpy as np
import pandas as pd

import pymc as pm

import matplotlib.pyplot as plt

from pymc_marketing.plot import plot_curve

seed = sum(map(ord, "Arbitrary curve"))
rng = np.random.default_rng(seed)

dates = pd.date_range("2024-01-01", periods=52, freq="W")

coords = {"date": dates, "product": ["A", "B"]}
with pm.Model(coords=coords) as model:
    data = pm.Normal(
        "data",
        mu=[-0.5, 0.5],
        sigma=1,
        dims=("date", "product"),
    )
    cumsum = pm.Deterministic(
        "cumsum",
        data.cumsum(axis=0),
        dims=("date", "product"),
    )
    idata = pm.sample_prior_predictive(random_seed=rng)

curve = idata.prior["cumsum"]

fig, axes = plot_curve(
    curve,
    "date",
    subplot_kwargs={"figsize": (15, 5)},
    random_seed=rng,
)
plt.show()

(Source code, png, hires.png, pdf)

../../_images/pymc_marketing-plot-plot_curve-1.png

Choose the HDI intervals and number of samples

fig, axes = plot_curve(
    curve,
    "date",
    n_samples=3,
    hdi_probs=[0.5, 0.95],
    random_seed=rng,
)
fig.suptitle("Same data but fewer lines and more HDIs")
plt.show()

(Source code, png, hires.png, pdf)

../../_images/pymc_marketing-plot-plot_curve-2.png

Plot same curve on same axes with custom colors

colors = ["red", "blue"]
fig, axes = plot_curve(
    curve,
    "date",
    same_axes=True,
    colors=colors,
    random_seed=rng,
)
axes[0].set(title="Same data but on same axes and custom colors")
plt.show()

(Source code, png, hires.png, pdf)

../../_images/pymc_marketing-plot-plot_curve-3.png