I want the x ticks to have the pairs of (position, topic)
nothing in the y ticks
Assistant
Here’s an example of Python + seaborn code that takes a dataframe with columns:
text (or any text field — unused for plotting)
topic (categorical)
position (numeric timeline position)
and produces a scatterplot where:
Each point = a topic
X-axis = average position of that topic
Point size = relative frequency of the topic
Y-axis = just spreads topics for visibility (or can be removed)
You can paste this into your script and adapt the column names as needed.
User
What can I do to make the labels not ontop of eachother?
Assistant
Got it — you want your scatterplot to:
show the (position, topic) pairs as tick labels on the x-axis,
and remove all y-axis ticks.
You can do this by customizing the tick labels after plotting, using set_xticks() and set_xticklabels().
Here’s how you can modify your existing code 👇
pythonCopy codeimport matplotlib.pyplot as plt
import seaborn as sns
assume topic_summary has columns: ['topic', 'avg_position', 'size']
# 2. Normalize size for plotting (optional)
size_scale = 0.1 / topic_summary["frequency"].max()
topic_summary["size"] = topic_summary["frequency"] * size_scale
# 3. Create scatterplot
plt.figure(figsize=(24, 3))
ax = sns.scatterplot(
data=topic_summary,
x="avg_position",
y=1,
size="size",
sizes=(20, 800),
legend=False
)
# --- Remove y-axis ticks ---
ax.set_yticks([])
# y limit
ax.set_ylim(0.8, 1.2)
# --- Add x-axis ticks as (position, topic) pairs ---
pairs = [f"{round(pos, 2)}\n{topic}" for pos, topic in
zip(topic_summary["avg_position"], topic_summary["en_name"])]
# ax.set_xticks(topic_summary["avg_position"])
# ax.set_xticklabels(pairs, rotation=30, ha='right', fontsize=8)
# ax.set_xticklabels([round(pos, 2) for pos in topic_summary["avg_position"]],
# ha='right', fontsize=16)
for _, row in topic_summary.iterrows():
ax.text(row["avg_position"], 0, # adjust y as needed
f"{row['en_name'].split(',')[0]}",
rotation=45, ha='left', va='bottom', fontsize=16)
ax.set_xticks([])
ax.set_yticks([])
# --- Labels and title ---
ax.set_xlabel("Average Position (with Topic)")
ax.set_ylabel("")
ax.set_title("Topic Timeline Overview (Position vs. Topic Frequency)")
plt.tight_layout()
plt.show()
Assistant
Good question — when you have many x-axis labels (especially long ones like (0.43, kibbutz, year, community)), they overlap easily.
Here are five reliable ways to make them readable — you can mix or choose one depending on your preferences:
✅ Option 1: Rotate and spread out the ticks
Increase figure width and rotate labels steeply:
pythonCopy codeplt.figure(figsize=(16, 4)) # wider figure
sns.scatterplot(...)
User
I'm doing this to adress a peer review on a topic modeling digital history article.
This is the comment: "I was hoping to see a visualization of the whole corpus that wasn’t just a bar graph of topic differences by gender – is there any way to “see” all the topics and visualize their relationships or frequency? "
Can you give me an idea?
Assistant
Good question — your code is almost perfect, but there are a couple of subtle issues that explain why the annotations (text labels) may not appear as expected or overlap strangely.
Let’s break down the likely problems 👇
User
please implement “Topic Landscape” Heatmap
Assistant
User
If num_bins=20 we get a square matrix?
Assistant
User
This gave me 0 for all bin edges:
bin_edges = pd.cut(df["position"], bins=num_bins, retbins=True)[1]
plt.xticks(
ticks=np.arange(num_bins),
labels=[f"{int(b)}" for b in bin_edges[:-1]],
rotation=45
)
Assistant
Excellent question — and that’s a subtle one 👏
👉 No, setting num_bins = 20 does not make the matrix square.
Let’s unpack what’s going on.
🧮 What determines the matrix shape
When we create:
pythonCopy codeheatmap_data = df.groupby(["topic", "pos_bin"]).agg(...).pivot(index="topic", columns="pos_bin")
The rows come from unique values of topic
The columns come from the number of position bins (num_bins, if all are used)
So:
#rows = number of topics
#columns = up to num_bins (some bins might be empty)
✅ Example:
topicsbins15 topics20 bins → 15 × 20 matrix ✅35 topics20 bins → 35 × 20 matrix ✅
Only if you happen to have 20 topics and 20 bins will it be square — but that’s just coincidence.
🧩 Interpreting the axes
X-axis (columns) = timeline / position segments (bins)
Y-axis (rows) = topics
Each cell = topic intensity in that timeline segment
So, num_bins simply controls horizontal resolution of your timeline:
small (e.g., 10) → broad chronological phases
User
I want to display every other label: 0.00, 0.10, 0.2, ...
Assistant
Ah — good catch! 👏
That “all zeros” behavior usually means your position values aren’t distributed as pd.cut() expects — most likely they’re very small (e.g. 0–1 normalized) or non-integer/floats that get truncated when you do int(b).
Let’s go step by step.
🧭 Why it happens
pd.cut(df["position"], bins=num_bins, retbins=True)
returns bin edges — numeric values dividing the range of your positions.
So if your positions are, for example:
Copy code0.00, 0.01, 0.03, ..., 0.98, 1.00
then the bin edges are like:
scssCopy codearray([0.00, 0.05, 0.10, 0.15, ..., 1.00])
When you do:
pythonCopy code[int(b) for b in bin_edges[:-1]]
each int(b) rounds down to 0 (since all b < 1), giving [0, 0, 0, …].
User
explain:
3. Normalize per-topic (optional, for visual contrast)
Perfect 👍 — that’s exactly what you want for a readable timeline axis.
Assuming your bin edges are in a numeric range like [0.00, 0.05, 0.10, …, 1.00],
you can simply slice every other label before passing them to plt.xticks().
Here’s the cleanest version 👇
✅ Show every other tick label
pythonCopy codebin_edges = pd.cut(df["position"], bins=num_bins, retbins=True)[1]
every other edge (0, 2, 4, ...)
step = 2
tick_positions = np.arange(num_bins)[c949:a31b:6358:c8dd:f7af:c2be:7cd:8d0dstep]
tick_labels = [f"{b:.2f}" for b in bin_edges[:-1][c949:a31b:6358:c8dd:f7af:c2be:7cd:8d0dstep]]
np.arange(num_bins) gives tick indices [0, 1, 2, …, num_bins-1]
[c949:a31b:6358:c8dd:f7af:c2be:7cd:8d0dstep] selects every 2nd one
labels are formatted to two decimals (0.00, 0.10, 0.20, …)
You can change step to 3 or 4 for sparser labeling if your heatmap is wide.
✅ Optional: If you want 0.00 → 1.00 cleanly spaced
You can even generate your own labels directly without reading from bin_edges:
pythonCopy codetick_positions = np.arange(0, num_bins, 2)
tick_labels = [f"{i/num_bins:.2f}" for i in tick_positions]
plt.xticks(tick_positions, tick_labels, rotation=45)
User
How to make font of Topic Intensity larger?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
plt.figure(figsize=(12, 4))
sns.scatterplot(
data=topic_summary,
x="avg_position",
y="topic", # or use y=0 to force a single horizontal row
size="size",
sizes=(20, 800),
legend=False
)
plt.xlabel("Average Position on Narrative Timeline")
plt.ylabel("Topic")
plt.title("Topic Timeline Overview (Position vs. Topic Frequency)")
plt.tight_layout()
plt.show()
✅ Notes & Options
If you want the topics all on one horizontal line (true timeline style):
✅ Best when you have <20 topics
🟡 Still crowded if positions are close.
✅ Option 2: Use fewer x-ticks (sample or skip)
Show only every nth label:
pythonCopy codestep = 2 # show every 2nd label
positions = topic_summary["avg_position"].iloc[c949:a31b:6358:c8dd:f7af:c2be:7cd:8d0dstep]
labels = [pairs[i] for i in range(0, len(pairs), step)]
✅ Keeps labels readable
🟡 You lose some topics on the x-axis
✅ Option 3: Shorten topics (first word only)
If your topics are comma-separated, just show the first term:
pythonCopy codepairs = [f"({round(pos, 2)}, {topic.split(',')[0]})"
for pos, topic in zip(topic_summary["avg_position"], topic_summary["topic"])]
✅ Clean and compact
🟢 Recommended for narrative/topic visualization
✅ Option 4: Use vertical labels
Rotate labels vertically (rotation=90) to save horizontal space:
pythonCopy codeax.set_xticklabels(pairs, rotation=90, fontsize=8)
✅ Simple fix
🟡 Harder to read at a glance, but great for dense plots.
✅ Option 5: Use annotations above each point instead of x-ticks
Remove tick labels entirely and label each bubble:
pythonCopy codefor _, row in topic_summary.iterrows():
ax.text(row["avg_position"], 0, # adjust y as needed
f"{row['topic'].split(',')[0]}",
rotation=45, ha='left', va='bottom', fontsize=8)
ax.set_xticks([])
ax.set_yticks([])
✅ Looks cleaner, especially for a horizontal timeline layout.
Would you like the timeline to look more like a horizontal narrative line (all topics aligned at y=0) instead of stacked by topic?
If yes, I can give you a version where the bubbles sit on a single axis with labels placed above each bubble — much more readable visually.
⚠️ 1. y=1 in the scatterplot means your dots are at y=1,
but your annotation text is drawn at y=0
pythonCopy codeax.text(row["avg_position"], 0, ...)
That’s below your y-limits:
pythonCopy codeax.set_ylim(0.8, 1.2)
→ So, the text is outside the visible range and doesn’t show up.
✅ Fix:
Use a y-value slightly below the dots, but still visible (e.g. 0.92):
pythonCopy codefor _, row in topic_summary.iterrows():
ax.text(
row["avg_position"], 0.92, # <--- inside range
f"{row['en_name'].split(',')[0]}",
rotation=45, ha='left', va='top', fontsize=10
)
⚠️ 2. y=1 as a constant works, but Seaborn will add a tiny random jitter
because y is numeric but constant.
You can disable that jitter by explicitly setting hue=None and alpha=1.
✅ Example:
pythonCopy codeax = sns.scatterplot(
data=topic_summary,
x="avg_position",
y=[1]*len(topic_summary), # force exact constant y
size="size",
sizes=(20, 800),
legend=False
)
⚠️ 3. Font size and rotation can cause text overlap
You’re using fontsize=16, which is quite large for horizontal annotations.
✅ Try:
pythonCopy codefontsize=9
rotation=60
ha='right'
✅ Cleaned-up, fully working version
pythonCopy codeplt.figure(figsize=(24, 3))
1. Plot
ax = sns.scatterplot(
data=topic_summary,
x="avg_position",
y=[1]*len(topic_summary), # force flat line
size="size",
sizes=(20, 800),
legend=False
)
for _, row in topic_summary.iterrows():
ax.text(
row["avg_position"], 0.92,
f"{row['en_name'].split(',')[0]}",
rotation=60, ha='right', va='top', fontsize=9
)
4. Title and labels
ax.set_xlabel("Average Position on Narrative Timeline")
ax.set_ylabel("")
ax.set_title("Topic Timeline Overview (Position vs. Topic Frequency)")
plt.tight_layout()
plt.show()
✅ Result:
All dots are on a clean horizontal line
Labels appear below or above each dot
No overlapping y-ticks or extra jitter
Much more readable, compact timeline-style visualization
Would you like me to make the labels auto-adjust up/down (so that neighboring ones don’t collide)? That’s possible with a short offset logic.
That’s an excellent and very typical reviewer request — they’re essentially asking for a global visualization of your topic model, one that captures either:
relationships between topics (semantic or co-occurrence proximity),
frequency or prominence across the corpus, or
ideally, both in a single figure.
Here are several visual ideas (with a short explanation of when and why each works well for digital humanities or historical corpora):
🌐 1. Topic Relationship Map (2D Embedding of Topic Vectors)
Goal: “See” all topics in semantic space.
How:
Take each topic’s word distribution vector or embedding centroid (if using contextual models like BERTopic or top2vec).
Reduce to 2D using UMAP or t-SNE.
Plot with scatter/bubble size = topic frequency, color = thematic cluster (if applicable).
Result:
A map where nearby bubbles represent semantically related topics — e.g. "family/home" cluster near "children/school", while "war/security" sits elsewhere.
This directly answers “visualize their relationships”.
Toolkits:
umap-learn + matplotlib / seaborn
pyLDAvis (classic LDA interactive visualization)
BERTopic.visualize_topics() (interactive, modern)
Scholarly advantage:
Looks interpretive, not purely technical — reviewers see conceptual proximity, not just counts.
🧭 2. Topic Timeline (what you’re building now)
Goal: “See” topic movement or placement along the corpus’s narrative or temporal axis.
This already fits digital history beautifully if your corpus is diachronic or narrative (e.g., testimonies, diaries, letters).
You can:
Plot topics along the timeline (average position or median date).
Bubble size = frequency / salience.
Color = thematic category (if manually assigned).
Optional refinement: overlay major historical periods as shaded bands.
Scholarly framing:
“This timeline visualization highlights how narrative attention shifts across themes — e.g., topics of alertness and shelter dominate early segments, while memory and faith emerge later.”
🕸️ 3. Topic Co-occurrence Network
Goal: Show relationships via shared documents.
How:
Compute topic co-occurrence across documents (topics that appear together often).
Build a network: nodes = topics, edges = co-occurrence strength.
Size nodes by frequency; color by thematic cluster.
Tools:
networkx + matplotlib or pyvis for interactive HTML export.
Scholarly framing:
“The network reveals two primary thematic hubs: collective memory topics co-occurring with religious reflection, and community/war-related topics forming a separate cluster.”
🧩 4. Stacked Distribution Across Metadata
Goal: Keep frequency but show it richer than bar charts.
Example: stacked area or streamgraph showing topic proportions over time, gender, or region.
Tools: plotly.express.area, matplotlib.stackplot.
Good for: diachronic or demographic comparisons — very intuitive for reviewers used to quantitative DH graphics.
🗺️ 5. “Topic Landscape” Heatmap
Goal: Blend textual themes and chronology in a compact form.
Each row = topic; each column = document position or year.
Color intensity = topic weight.
→ You see which topics dominate when.
Scholarly framing:
“This heatmap situates the emergence and fading of topics across testimonies, illustrating transitions from descriptions of danger to reflections on faith and survival.”
💡 What I’d recommend for your revision
Since your article is digital history and reviewers already saw bar graphs by gender, the most impactful addition would be either:
🕸️ A topic co-occurrence network — shows relationships across the corpus,
→ visually distinct from the bar chart, conceptually richer.
🌐 A semantic topic map (UMAP) — shows proximity in meaning space,
→ feels “big-picture,” helps them “see the whole corpus.”
🧭 The topic timeline you’re already developing —
→ perfect if your corpus is narrative or temporally ordered (e.g., testimonies, diaries).
Would you like me to generate a code template for one of those (e.g., the topic network or semantic map) based on your current topic_summary or topic-word data?
That way you could include it directly in your revision figure.
Perfect — the “Topic Landscape” Heatmap is one of the most elegant ways to “see” the entire topic model across your corpus.
It’s interpretive (historians love it), compact, and looks great in an article.
Let’s walk through exactly how to implement it.
🧭 Goal
Visualize:
Rows = topics
Columns = document positions or chronological segments
Color = topic prominence (average weight)
This works whether your “position” column is:
a numeric timeline index (e.g., 0–1 normalized), or
an actual date/year (which can be binned).
✅ Example Implementation in Python (Seaborn)
pythonCopy codeimport pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
plt.xlabel("Narrative / Chronological Position")
plt.ylabel("Topic")
plt.title("Topic Landscape: Distribution of Topics Across the Corpus")
plt.tight_layout()
plt.show()
🧩 Result:
Each row shows a topic (e.g., "alertness, soldier, section")
Each column = a slice of your corpus timeline (e.g., first → last segment)
Color intensity = relative strength of the topic in that segment.
You can visually trace topic arcs — e.g., a “war” topic that dominates early, then fades as “memory” rises.
💬 Optional Refinements
🔹 Add chronological x-axis
If position is a year or date, replace bin labels:
pythonCopy codebin_edges = pd.cut(df["position"], bins=num_bins, retbins=True)[1]
plt.xticks(
ticks=np.arange(num_bins),
labels=[f"{int(b)}" for b in bin_edges[:-1]],
rotation=45
)
🔹 Sort topics by temporal center of mass
To make the plot narrative-like (early-to-late themes):
pythonCopy codetopic_order = heatmap_data.apply(lambda x: np.argmax(x.values), axis=1).sort_values().index
sns.heatmap(heatmap_data.loc[topic_order], ...)
🪶 Scholarly framing for your revision:
You could caption it like:
Figure X. Topic Landscape Heatmap showing how each topic’s prominence varies along the corpus timeline.
Early sections focus on war and shelter, while later portions emphasize memory, reflection, and identity.
Would you like me to adapt the code directly to your variable names (e.g. en_name, position, weight or frequency)? That way it’ll run immediately on your dataset._
large (e.g., 50) → fine-grained progression
💡 TL;DR
SettingEffectnum_binsnumber of timeline slices (x-axis width)#topicsnumber of rows (y-axis height)Not squareNormal — that’s how the landscape should look!
Would you like me to show how to automatically choose num_bins based on corpus length (e.g., one bin per 50 documents or per 2% of corpus)? That gives a visually balanced heatmap without guessing._
✅ Fix #1 — Format as floats instead of ints
Show a couple of decimals:
pythonCopy codebin_edges = pd.cut(df["position"], bins=num_bins, retbins=True)[1]
plt.xticks(
ticks=np.arange(num_bins),
labels=[f"{b:.2f}" for b in bin_edges[:-1]],
rotation=45
)
That will produce labels like:
Copy code0.00, 0.05, 0.10, 0.15, ...
✅ Fix #2 — If position is actually chronological (e.g. year)
Then you can convert edges to integers only if the values are large enough:
pythonCopy codebin_edges = pd.cut(df["position"], bins=num_bins, retbins=True)[1]
plt.xticks(
ticks=np.arange(num_bins),
labels=[str(int(round(b))) for b in bin_edges[:-1]],
rotation=45
)
✅ Fix #3 — If you just want bin indices (not actual values)
You can label bins simply as segments:
pythonCopy codeplt.xticks(
ticks=np.arange(num_bins),
labels=[f"Seg {i+1}" for i in range(num_bins)],
rotation=45
)
TL;DR
SituationWhat to useposition ∈ [0, 1]f"{b:.2f}"position are yearsint(round(b))just want labeled bins"Seg {i+1}"
Would you like me to show how to adapt the heatmap so the x-axis labels correspond to document indices or years automatically (depending on what position represents in your corpus)?
Would you like me to show how to automatically choose the step based on figure width (so labels never overlap)?
Excellent — that’s a really smart question, and it goes to the heart of how we make topic heatmaps meaningful but still readable.
Let’s unpack it carefully 👇
🧩 What the line does
pythonCopy codeheatmap_data = heatmap_data.div(heatmap_data.max(axis=1), axis=0)
This divides each row (each topic) by its own maximum value.
Mathematically, for topic t and bin b:
normalized(t,b)=weight(t,b)maxbweight(t,b)\text{normalized}(t, b) = \frac{\text{weight}(t, b)}{\max_b \text{weight}(t, b)}normalized(t,b)=maxbweight(t,b)weight(t,b)
So within each topic row:
the most dominant cell = 1.0
everything else = proportionally less (0–1 scale).
🧭 Why it’s legit (and often desirable)
The goal of a heatmap like this isn’t to show absolute frequency — it’s to reveal the shape of each topic’s trajectory across the corpus.
If we don’t normalize:
very frequent topics (like “war” or “family”) dominate the color scale,
rare but narratively significant topics (like “faith” or “escape”) look invisible (all dark blue).
After normalization:
each topic’s row uses its own internal contrast,
so you can visually compare when each topic peaks — not how much it appears overall.
This makes the heatmap visually expressive:
it highlights temporal pattern, not absolute magnitude.
📊 Analogy
Think of it like this:
Without normalizationWith per-topic normalization“Loud” topics drown out subtle onesEach topic gets an equal chance to show its shapeColor scale shared globallyColor scale adapted per topicGood for quantitative comparisonGood for qualitative trajectory visualization
⚖️ When it’s not legit
If your interpretive goal is to compare across topics quantitatively (e.g., “topic A appears twice as much as topic B”),
then per-topic normalization distorts those proportions.
In that case, you should skip the line and instead normalize globally:
pythonCopy codeheatmap_data = heatmap_data / heatmap_data.values.max()
That preserves absolute differences.
✅ Summary
QuestionAnswerWhat does it do?Scales each topic row by its own max valueWhy do it?To equalize visibility and show relative variation across the corpusWhen is it legit?When the interpretive aim is pattern comparison (not frequency comparison)When to avoid?If you want absolute cross-topic comparisons
So in your article context —
for a digital history piece focused on narrative trajectories or thematic evolution,
this normalization is absolutely legitimate and methodologically justified.
You can even phrase it like this in your methods section:
“For visualization purposes, topic intensities were normalized per topic to highlight relative changes in prominence along the corpus timeline.”
Would you like me to help you phrase a one-sentence footnote or caption justification for this choice (so reviewers see it’s methodologically sound)?