-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plot): create plot for line, histogram, and gen pareto distribution
- Loading branch information
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import typing | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import pandas as pd | ||
import scipy.stats as stats | ||
|
||
|
||
def plot_line( | ||
dataset: typing.Union[pd.DataFrame, pd.Series], | ||
threshold: typing.Union[pd.DataFrame, pd.Series, float], | ||
title: str, | ||
xlabel: str, | ||
ylabel: str, | ||
is_threshold: bool = True, | ||
plot_width: int = 13, | ||
plot_height: int = 8, | ||
plot_color: str = "black", | ||
th_color: str = "red", | ||
th_type: str = "dashed", | ||
th_line_width: int = 2, | ||
alpha: float = 0.8, | ||
): | ||
fig = plt.figure(figsize=(plot_width, plot_height)) | ||
plt.plot(dataset.index, dataset.values, color=plot_color, alpha=alpha) | ||
|
||
if is_threshold: | ||
plt.axhline(threshold, c=th_color, ls=th_type, lw=th_line_width) | ||
plt.title(title) | ||
plt.xlabel(xlabel) | ||
plt.ylabel(ylabel) | ||
|
||
fig.legend(loc="upper left", shadow=True, fancybox=True) | ||
plt.show() | ||
|
||
|
||
def plot_hist( | ||
dataset: typing.Union[pd.DataFrame, pd.Series], | ||
title: str, | ||
xlabel: str, | ||
ylabel: str, | ||
bins: int = 50, | ||
plot_width: int = 13, | ||
plot_height: int = 8, | ||
plot_color: str = "black", | ||
alpha: float = 0.8, | ||
): | ||
fig = plt.figure(figsize=(plot_width, plot_height)) | ||
plt.hist(dataset.values, bins=bins, color=plot_color, alpha=alpha) | ||
plt.title(title) | ||
plt.xlabel(xlabel) | ||
plt.ylabel(ylabel) | ||
|
||
fig.legend(loc="upper left", shadow=True, fancybox=True) | ||
plt.show() | ||
|
||
|
||
def plot_gen_pareto( | ||
dataset: typing.Union[pd.DataFrame, pd.Series], | ||
title: str, | ||
xlabel: str, | ||
ylabel: str, | ||
bins: int = 50, | ||
plot_width: int = 13, | ||
plot_height: int = 8, | ||
plot_color: str = "black", | ||
alpha: float = 0.8, | ||
params: typing.Union[typing.Dict, None] = None, | ||
): | ||
fig = plt.figure(figsize=(plot_width, plot_height)) | ||
|
||
nonzero_exceedences = [exceedence for exceedence in dataset if exceedence > 0] | ||
if params: | ||
param_label = f"\n{round(params['c'], 3)}\n{round(params['loc'], 3)}\n{round(params['scale'], 3)}\n" | ||
overlay = np.linspace( | ||
stats.genpareto.ppf(0.1, c=params["c"], loc=params["loc"], scale=params["scale"]), | ||
stats.genpareto.ppf(0.999, c=params["c"], loc=params["loc"], scale=params["scale"]), | ||
len(nonzero_exceedences), | ||
) | ||
plt.plot( | ||
overlay, | ||
stats.genpareto.pdf(overlay, c=params["c"], loc=params["loc"], scale=params["scale"]), | ||
c="lime", | ||
lw=2, | ||
label=f"\nFitted Params:{param_label}", | ||
) | ||
plt.hist( | ||
nonzero_exceedences, | ||
bins=bins, | ||
density=True, | ||
alpha=alpha, | ||
color=plot_color, | ||
label=f"{len(nonzero_exceedences)}", | ||
) | ||
plt.xlabel(xlabel) | ||
plt.ylabel(ylabel) | ||
plt.title(title) | ||
fig.legend(loc="upper right", shadow=True, fancybox=True) | ||
plt.show() |