Skip to content
svgtopic-terms

Topic terms

What is each topic about?

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

How to read it

  1. Read each panel's top terms to name the topic.
  2. Compare bar lengths: the most representative terms.
  3. A term in several panels doesn't distinguish topics.
  1. Callout 1: This topic's top term.
  2. Callout 2: The topic's label, for you to check.
  3. Callout 3: Bars: the term's weight.

Watch out

  • Topics are bags of words: the name you give them is your interpretation.

Defaults

Terms per topic
10
How many terms each topic shows.
Range 3 to 30, step 1

Pinned in the code

  • Draws the run's numbers as given (computed by the host's cs-ml evaluation or the Python/R engine); the chart computes nothing statistical.
  • Bars are scaled within each panel (the largest term fills the panel); weights are not comparable across topics for NMF.
# Topic terms — Codesplash Chart Lab (recipe "topic-terms")
# controls: top=10
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("topic-terms-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["topic"] = pd.Categorical(df["topic"].astype("string"), categories=["Topic 1 · pricing", "Topic 2 · delivery", "Topic 3 · quality", "Topic 4 · service"])

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)))
TOPIC = "topic"
TERM = "term"
W = "P(word | topic)"
TOP = 10  # terms per topic
d = df.dropna(subset=[TOPIC, TERM, W])
topics = levels_of(d[TOPIC])
ncol = min(4, len(topics))
nrow = int(np.ceil(len(topics) / ncol))
fig, axes = plt.subplots(nrow, ncol, figsize=(3.2 * ncol + 1, 0.28 * TOP * nrow + 1.2), squeeze=False)
for i, ax in enumerate(axes.flat):
    if i >= len(topics):
        ax.axis("off")
        continue
    g = d[d[TOPIC].astype(str) == str(topics[i])].nlargest(TOP, W).iloc[::-1]
    ax.barh(g[TERM].astype(str), g[W], color=PALETTE[i % len(PALETTE)])
    ax.set_title(str(topics[i]), fontsize=9)
    ax.tick_params(labelsize=8)
fig.supxlabel(W)
fig.tight_layout()
plt.show()

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