Skip to content
svgtopic-sizes

Topic sizes

How large is each topic, and how many documents fit no topic at all?

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

How to read it

  1. Read each bar as one topic: its length is how many documents fell into it, and its label lists the words that set it apart.
  2. Topics are ordered from largest to smallest; the grey bar at the end holds the documents that fit no topic.
  3. Check the grey bar first: a long one means many documents were left unassigned by the settings.
  1. Callout 1: The largest topic and its distinctive words.
  2. Callout 2: Outliers: documents in no dense group.
  3. Callout 3: Class-based TF-IDF words: frequent here, rare in other topics.

Watch out

  • Topic sizes depend on the smallest topic size and the seed; rerun with other settings before trusting a small topic.
  • Distinctive words describe the topic, not every document in it.

Defaults

No settings: the chart draws one way.

Pinned in the code

  • Draws what the Python or R engine computed; the chart computes nothing statistical.
  • Topics are ordered by size; the outlier group (HDBSCAN noise, topic −1) is drawn grey and last whatever its size.
  • Bar labels are the topic's highest class-based TF-IDF words — words frequent in this topic and rare in the others.
  • A large outlier bar means the density clustering left many documents unassigned; it is a property of the settings (smallest topic size), not of the documents alone.
# Topic sizes — Codesplash Chart Lab (recipe "topic-sizes")
# controls: defaults
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-sizes-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"])  # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"])  # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
TOPIC = "topic"
DOCS = "documents"
TERMS = "top terms"
OUT = "outliers"
d = df.dropna(subset=[DOCS]).copy()
d["_out"] = d[OUT].astype(bool) if OUT else d[TOPIC].astype(str).str.fullmatch(r"(?i)outliers?|-1|noise")
d = d.sort_values(["_out", DOCS], ascending=[True, False], kind="stable").reset_index(drop=True)  # outliers last
fig, ax = plt.subplots(figsize=(8, 0.45 * len(d) + 1.2))
y = np.arange(len(d))[::-1]
ax.barh(y, d[DOCS], color=[GREY if o else PALETTE[0] for o in d["_out"]])
if TERMS:
    for yi, n, t in zip(y, d[DOCS], d[TERMS].fillna("")):
        ax.annotate(str(t), (n, yi), xytext=(4, 0), textcoords="offset points", va="center", fontsize=8)
ax.set_yticks(y, d[TOPIC].astype(str))
ax.set_xlabel(DOCS)
ax.set_title("Documents per topic (outliers last)")
plt.show()

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