Visualizing medical code hierarchy with treemaps
(832) 422-8646
Contact
Quick follow up to the previous two posts on ICD-10 codes and HCPCS codes. This post uses Python’s squarify library to create treemaps visualizing how many codes begin with each letter.
Here’s the treemap for HCPCS codes.
And here’s the treemap for ICD-10 codes.
The sizes of the squares are proportional to the number of codes beginning with that letter. Note that they are not necessarily proportional to how often codes are used.
The HCPCS map omits R and U because these are tiny relative to the rest. The ICD-10 map omits U for the same reason.
Here’s the code that was used to create the HCPCS map.
import matplotlib.pyplot as plt<br>import squarify
# HCPCS<br>data = {<br>"G": 2010,<br>"J": 1232,<br>"L": 940,<br>"A": 862,<br>"E": 671,<br>"Q": 639,<br>"C": 619,<br>"S": 533,<br>"M": 506,<br>"V": 212,<br>"K": 175,<br>"T": 114,<br>"H": 94,<br>"P": 59,<br>"B": 51,<br># "U": 5,<br># "R": 3,
labels = list(data.keys())<br>sizes = list(data.values())
# Labels are just the letters (no counts)<br>display_labels = labels
# Color map — one distinct color per box<br>colors = plt.cm.tab20.colors[: len(labels)]
fig, ax = plt.subplots(figsize=(12, 8))<br>squarify.plot(<br>sizes=sizes,<br>label=display_labels,<br>color=colors,<br>alpha=0.85,<br>ax=ax,<br>text_kwargs={"fontsize": 30, "weight": "bold"},<br>pad=True,<br>ax.axis("off")
plt.tight_layout()<br>plt.savefig("treemap.png", dpi=72)<br>plt.show()
The code to create the ICD-10 map differs only in its data.
# ICD-10<br>data = {<br>"S": 31052,<br>"T": 10090,<br>"M": 6665,<br>"V": 4086,<br>"H": 3330,<br>"O": 2437,<br>"Y": 1590,<br>"I": 1427,<br>"Z": 1411,<br>"W": 1290,<br>"C": 1226,<br>"L": 1000,<br>"E": 971,<br>"Q": 894,<br>"F": 871,<br>"K": 857,<br>"N": 836,<br>"D": 824,<br>"R": 773,<br>"G": 700,<br>"A": 573,<br>"X": 495,<br>"B": 495,<br>"P": 463,<br>"J": 360,<br># "U": 3,
One thought on “Visualizing Medical Code Hierarchy”
Eric
17 July 2026 at 12:29
One of the key ideas of a good medical nomenclature system is to NOT encode parent-child relationships in the identifier itself because that limits the ability to expand to include new terms or deprecate old terms. The SNOMED hierarchy is more than a tree and allows concepts to have multiple parents. This is one reason that for research SNOMED codes are much more useful than ICD and CPT codes. This makes it harder to show in a simple treemap, but a directed graph (using perhaps GraphViz) would still work.
Comments are closed.
Search for:
John D. Cook, PhD
My colleagues and I have decades of consulting experience helping companies solve complex problems involving data privacy, applied math, and statistics.
Let’s talk. We look forward to exploring the opportunity to help your company too.