Commit aaaacff2 authored by Leodegario Lorenzo II's avatar Leodegario Lorenzo II
Browse files

Add explained variance plot

parent bd6ff23d
from .word_cloud import word_cloud
from ._word_cloud import word_cloud
from ._exp_var import exp_var
import numpy
import matplotlib.pyplot as plt
def exp_var(exp_var_ratio, tol=0.90):
"""Return explained variance plot given explained variance ratio
exp_var_ratio : numpy array
Array containing the percentage of variance explained by
each of the singular vectors.
tol : float, default=0.90
Default tolerance value for the optimal threshold
fig, ax : matplotlib figure and axes
Figure and axes of the plot
# Get cumsum of explained variance ratio
exp_var = (exp_var_ratio).cumsum()
# Get index where exp_var exceeds tolerance
thresh = np.min(np.arange(len(s))[exp_var >= tol])
# Initialize figure
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111)
# Plot explained varianced
ax.plot(range(len(s)), exp_var, lw=4.0)
# Plot threshold line
ax.axvline(thresh, linestyle='--', lw=2.5, color='tab:orange')
# Annotate threshold
trans = transforms.blended_transform_factory(ax.transData, ax.transAxes)
ax.text(1.05*thresh, 0.05, f"Threshold: {int(thresh)}", color='tab:orange',
weight='bold', fontsize=12, transform=trans)
# Set ylim
ax.set_ylim([min(exp_var), 1.05])
ax.set_xlim([0., len(s)])
# Remove spines
# Set axis labels
ax.set_xlabel('Number of components', fontsize=14)
ax.set_ylabel('Cumulative explained variance', fontsize=14)
return fig, ax
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment