Skip to content

Commit

Permalink
add optional ax argument
Browse files Browse the repository at this point in the history
  • Loading branch information
wd60622 committed Aug 5, 2023
1 parent 12b56e2 commit 38fb5fa
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions pymc_marketing/clv/plotting.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

import matplotlib.pyplot as plt
import numpy as np

Expand All @@ -10,11 +12,12 @@
def plot_frequency_recency_matrix(
model,
t=1,
max_frequency=None,
max_recency=None,
title=None,
xlabel="Historical Frequency",
ylabel="Recency",
max_frequency: Optional[int] = None,
max_recency: Optional[int] = None,
title: Optional[str] = None,
xlabel: str = "Customer's Historical Frequency",
ylabel: str = "Customer's Recency",
ax: Optional[plt.Axes] = None,
**kwargs,
) -> plt.Axes:
"""
Expand Down Expand Up @@ -64,19 +67,23 @@ def plot_frequency_recency_matrix(
.mean(("draw", "chain"))
.values.reshape(mesh_recency.shape)
)
if ax is None:
ax = plt.subplot(111)

Check warning on line 71 in pymc_marketing/clv/plotting.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/clv/plotting.py#L70-L71

Added lines #L70 - L71 were not covered by tests

ax = plt.subplot(111)
pcm = ax.imshow(Z, **kwargs)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
if title is None:
title = (
"Expected Number of Future Purchases for {} Unit{} of Time,".format(
t, "s"[t == 1 :]
)
+ "\nby Frequency and Recency of a Customer"
)
plt.title(title)

ax.set(

Check warning on line 82 in pymc_marketing/clv/plotting.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/clv/plotting.py#L82

Added line #L82 was not covered by tests
xlabel=xlabel,
ylabel=ylabel,
title=title,
)

force_aspect(ax)

Expand All @@ -88,11 +95,12 @@ def plot_frequency_recency_matrix(

def plot_probability_alive_matrix(
model,
max_frequency=None,
max_recency=None,
title="Probability Customer is Alive,\nby Frequency and Recency of a Customer",
xlabel="Customer's Historical Frequency",
ylabel="Customer's Recency",
max_frequency: Optional[int] = None,
max_recency: Optional[int] = None,
title: str = "Probability Customer is Alive,\nby Frequency and Recency of a Customer",
xlabel: str = "Customer's Historical Frequency",
ylabel: str = "Customer's Recency",
ax: Optional[plt.Axes] = None,
**kwargs,
) -> plt.Axes:
"""
Expand Down Expand Up @@ -143,12 +151,16 @@ def plot_probability_alive_matrix(

interpolation = kwargs.pop("interpolation", "none")

ax = plt.subplot(111)
if ax is None:
ax = plt.subplot(111)

Check warning on line 155 in pymc_marketing/clv/plotting.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/clv/plotting.py#L154-L155

Added lines #L154 - L155 were not covered by tests

pcm = ax.imshow(Z, interpolation=interpolation, **kwargs)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)

ax.set(

Check warning on line 159 in pymc_marketing/clv/plotting.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/clv/plotting.py#L159

Added line #L159 was not covered by tests
xlabel=xlabel,
ylabel=ylabel,
title=title,
)
force_aspect(ax)

# plot colorbar beside matrix
Expand Down

0 comments on commit 38fb5fa

Please sign in to comment.