Skip to content
svgngram-bars

Top terms

Which words or phrases appear most in a text column, overall or by group?

Demo data · seed 20261101 · 20 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Read bars as the most frequent terms per group.
  2. Compare panels: what's distinctive to each group?
  3. Use TF-IDF to look past words common to all groups.
  1. Callout 1: The most frequent term in this group.
  2. Callout 2: The other group's top term.
  3. Callout 3: Rarer terms.

Watch out

  • Frequency isn't importance, and terms lose their context.

Defaults

Terms per panel
20
How many terms each panel shows.
Range 3 to 50, step 1

Pinned in the code

  • Draws the run's numbers as given (computed by the Python/R engine or the host's scorecard); the chart computes nothing statistical.
  • Counts of the same term within a group are summed; ties are broken alphabetically. Tokenization (lowercasing, stop words, n-gram length) is the engine's — see the method's options.
# Top terms — Codesplash Chart Lab (recipe "ngram-bars")
# controls: top=20
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

try:
    import codesplash as cs  # optional: the app's chart theme
except ImportError:
    cs = None
if cs:
    cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]

df = pd.read_csv("ngram-bars-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["rating"] = pd.Categorical(df["rating"].astype("string"), categories=["1–2 stars", "4–5 stars"])

def levels_of(s):
    """Declared level order (categoricals), else order of first appearance."""
    if isinstance(s.dtype, pd.CategoricalDtype):
        return [v for v in s.cat.categories if (s == v).any()]
    return list(dict.fromkeys(s.dropna().astype(str)))
TERM = "bigram"
N = "count"
G = "rating"
TOP = 20
groups = levels_of(df[G]) if G else [None]
fig, axes = plt.subplots(1, len(groups), figsize=(4 * len(groups) + 1, 5), squeeze=False)
for i, (ax, g) in enumerate(zip(axes[0], groups)):
    d = (df if g is None else df[df[G].astype(str) == str(g)]).groupby(TERM, observed=True)[N].sum().reset_index()
    d = d.sort_values([N, TERM], ascending=[False, True]).head(TOP).iloc[::-1]  # largest on top
    ax.barh(d[TERM].astype(str), d[N], color=PALETTE[i % len(PALETTE)])
    ax.set_title(str(g) if g is not None else "", fontsize=10, loc="left")
    ax.set_xlabel(N)
plt.show()

Needs pandas, numpy, matplotlib · reads ngram-bars-demo.csv · app theme from the optional codesplash helper