Allotaxonograph demo¶
Compute the rank-turbulence divergence between the bundled climate and finance corpora, list the top contributors, and save the allotaxonograph.
What it shows¶
- Building two
RankedLists from the demo corpus pair - The rank-turbulence divergence and its top per-type contributions
- Saving the two-panel allotaxonograph to a PNG
Run it¶
Rank-turbulence divergence (alpha=1/3): 0.267
Top contributors:
climate 0.036 (climate)
market 0.036 (finance)
carbon 0.030 (climate)
stock 0.030 (finance)
trade 0.026 (finance)
Saved allotaxonograph.png
Source¶
examples/allotaxonograph_demo.py
"""Allotaxonograph demo: rank-turbulence divergence and the rank-rank map.
Run with: uv run python examples/allotaxonograph_demo.py
Writes allotaxonograph.png to the current directory.
"""
from keyflux import RankedList, allotaxonograph, rtd
from keyflux.datasets import load_demo_pair
def main() -> None:
"""Compare the bundled climate and finance corpora and save the figure."""
focus, reference = load_demo_pair()
climate = RankedList.from_counts(focus, label="climate")
finance = RankedList.from_counts(reference, label="finance")
result = rtd(climate, finance, alpha=1.0 / 3.0)
print(f"Rank-turbulence divergence (alpha=1/3): {result.divergence:.3f}")
print("Top contributors:")
for contribution in result.contributions[:5]:
leans = "climate" if contribution.direction == "system1" else "finance"
print(f" {contribution.type:<12} {contribution.contribution:.3f} ({leans})")
fig = allotaxonograph(climate, finance, alpha=1.0 / 3.0)
fig.savefig("allotaxonograph.png", dpi=120)
print("\nSaved allotaxonograph.png")
if __name__ == "__main__":
main()