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

Add eda functions

parent be0bf7e7
from .word_cloud import word_cloud
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
def word_cloud(text, figsize=(17, 8), width=500, height=500,
stopwords=None, bg_color=None, ax=None,
**kwargs):
"""Generate wordcloud given text corpus
Parameters
----------
text : str
Text corpus to be plotted
figsize : tuple, default=(17, 8)
Figure size of the subplot
width : float, default=500
Desired width of the wordcloud
height : float, default=500
Desired height of the wordcloud
stopwords : iterable, default=None
Stopwords to be used, if None uses wordcloud's default
stopwords
bg_color : str, default=None
Background color to be used. If None, sets this as
"rgba(255, 255, 255, 0)"
ax : matplotlib.Axes, default=None
Set axes if wc is to be plotted on an axes
**kwargs : other keyword arguments
Other keyword arguments to pass on the word cloud
Returns
-------
fig, ax : matplotlib figure and axes
Figure and axes of the subplots
"""
# Initialize subplots
if ax is None:
fig, ax = plt.subplots(figsize=figsize)
else:
fig = plt.gcf()
# Set stopwords
if stopwords is None:
stopwords = set(STOPWORDS)
if bg_color is None:
bg_color = "rgba(255, 255, 255, 0)"
# Initialize WordCloud class
wc = WordCloud(
background_color=bg_color,
stopwords=stopwords,
width=width,
height=height,
**kwargs
)
wc.generate(text)
# Generate visualization
ax.imshow(wc, interpolation='bilinear')
ax.axis('off')
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