chatviz.plotting.plot_words¶
- chatviz.plotting.plot_words(df, ax=None, top_n=10, stopwords=None, colors='default', show_titles=False)¶
Plots a bar chart per person with their top words used.
- Parameters
- dfpd.DataFrame
The dataframe of messages. Must have the columns [‘name’, ‘text’].
- axplt.Axes or None
This should be an iterable of M axes, where M is the number of unique names in df[‘name’]. If None (default), will create M new axes, via _, ax = plt.subplots(1, M).
- top_nint
The number of top words to include. Default is 10.
- stopwordsNone or iterable
If given, then these words will be removed from the plots. If None, all words will be kept (Note: this will lead to poor results as ‘the’, ‘and’, ‘a’, ‘is’ etc. will be the top words. A stopword list is recommended).
- colors{‘default’} or list of str or dict
The colors to be used for each person in the chat. Should be either ‘default’ in which case the default color scheme is used, a list of colors the same length as the number of names in df[‘name’], or a dict which maps each name to a color. For more info about color options, see here.
- show_titlesbool
If True, will show names about each plot. If False (default), then they will be hidden.
- Returns
- array of plt.Axes
The horizontal bar chart axes plots, one for each person in the chat.
Examples
from chatviz.utils import load_example_chat_data, STOPWORDS from chatviz.plotting import plot_words import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [30, 15] plt.rcParams["font.size"] = 25 plt.rcParams["axes.titlesize"] = 40 plt.rcParams["font.family"] = "Sawasdee" df = load_example_chat_data() palette = ["#20639B", "#3CAEA3", "#F6D55C", "#ED553B", "#173F5F"] plot_words(df, colors=palette, show_titles=True, stopwords=STOPWORDS) plt.suptitle("Most Used Words", size=50) plt.subplots_adjust(top=0.85, wspace=1.0) plt.show()
(Source code, png, hires.png, pdf)
The example below shows the results without stopwords being removed. In most cases the results with be better by removing stopwords, either using the chatviz.utils.STOPWORDS or a custom list of stopwords.
from chatviz.utils import load_example_chat_data, STOPWORDS from chatviz.plotting import plot_words import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [30, 15] plt.rcParams["font.size"] = 35 plt.rcParams["axes.titlesize"] = 40 plt.rcParams["font.family"] = "Sawasdee" df = load_example_chat_data() palette = ["#20639B", "#3CAEA3", "#F6D55C", "#ED553B", "#173F5F"] plot_words(df, colors=palette, show_titles=True, stopwords=None, top_n=5) plt.suptitle("Most Used Words", size=50) plt.subplots_adjust(top=0.85, wspace=1.0) plt.show()
(Source code, png, hires.png, pdf)