interactive decision tree
jupyter notebook
data visualization
machine learning
python

Plot Interactive Decision Tree in Jupyter Notebook

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Plotting a decision tree in Jupyter is easy. Making it interactive is the interesting part. A static plot_tree image is fine for small models, but once the tree gets deeper, you usually want sliders, zooming, or filtering so you can inspect the structure without constantly rerunning cells by hand.

One practical notebook-friendly approach is to combine scikit-learn's plot_tree with ipywidgets. That gives you a true interactive control surface without introducing a heavy visualization stack.

Train a Small Tree First

Start with a model that is easy to render:

python
1import matplotlib.pyplot as plt
2from sklearn.datasets import load_iris
3from sklearn.tree import DecisionTreeClassifier
4
5iris = load_iris()
6X = iris.data
7y = iris.target
8
9tree = DecisionTreeClassifier(max_depth=4, random_state=0)
10tree.fit(X, y)

You now have a standard scikit-learn tree object. The next step is giving yourself a way to explore it interactively.

Add an Interactive Depth Slider

Jupyter becomes interactive when you connect notebook widgets to a plotting function:

python
1import matplotlib.pyplot as plt
2from ipywidgets import IntSlider, interact
3from sklearn.tree import plot_tree
4
5
6def show_tree(depth):
7    plt.figure(figsize=(16, 8))
8    plot_tree(
9        tree,
10        feature_names=iris.feature_names,
11        class_names=iris.target_names,
12        filled=True,
13        rounded=True,
14        max_depth=depth,
15        fontsize=10,
16    )
17    plt.show()
18
19
20interact(show_tree, depth=IntSlider(min=0, max=4, step=1, value=2));

Now you can move the slider and inspect the tree layer by layer. This is often more useful than trying to stare at the full tree all at once.

Why This Counts as Interactive

The tree graphic itself is still rendered by Matplotlib, but the notebook experience is interactive because the widget changes the rendered view dynamically.

That is enough for many practical tasks:

  • exploring how the tree grows with depth
  • teaching decision tree concepts
  • inspecting feature names and split thresholds
  • debugging whether the model is already overfitting at shallow depths

For many notebooks, that is the best tradeoff between simplicity and usefulness.

Optional Richer Tools

If you want more advanced visuals, libraries such as dtreeviz can provide richer HTML-based output. Those tools often offer more polished node displays and nicer visual styling, but they also add setup complexity.

The notebook decision is usually:

  • use plot_tree plus ipywidgets for lightweight exploration
  • use a specialized library when you need presentation-quality visuals

In other words, start simple unless you have a real reason not to.

A Second Useful Interaction: Retrain by Depth

Sometimes you do not want to hide deeper nodes in the picture. You want to retrain the model itself at different depths and compare the learned structure.

python
1def train_and_show(max_depth):
2    model = DecisionTreeClassifier(max_depth=max_depth, random_state=0)
3    model.fit(X, y)
4
5    plt.figure(figsize=(16, 8))
6    plot_tree(
7        model,
8        feature_names=iris.feature_names,
9        class_names=iris.target_names,
10        filled=True,
11        rounded=True,
12        fontsize=10,
13    )
14    plt.show()
15
16
17interact(train_and_show, max_depth=IntSlider(min=1, max=6, step=1, value=3));

This lets you see how the actual fitted tree changes as the depth constraint changes, which is often more informative than clipping a single pre-trained tree.

Common Pitfalls

  • Expecting plot_tree by itself to be interactive. It is static until you connect it to notebook controls.
  • Rendering a huge unrestricted tree and wondering why the plot is unreadable.
  • Forgetting to install or enable ipywidgets support in the notebook environment.
  • Confusing "show part of the tree" with "retrain a smaller tree." Those are different workflows.
  • Making the figure too small, which causes labels and thresholds to overlap badly.

Summary

  • A simple way to plot an interactive decision tree in Jupyter is plot_tree plus ipywidgets.
  • A depth slider is often enough to make the tree explorable.
  • You can either clip a fitted tree visually or retrain the tree at different depths.
  • Start with lightweight notebook interactivity before reaching for heavier visualization libraries.
  • Readability matters more than visual complexity; even a basic interactive control can make tree debugging much easier.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.