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.
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:
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:
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_treeplusipywidgetsfor 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.
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_treeby 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
ipywidgetssupport 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_treeplusipywidgets. - 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
- Plot k-Nearest-Neighbor graph with 8 features?
- Plot learning curves with caret package and R
- Plot PCA loadings and loading in biplot in sklearn like R's autoplot
- Plot scikit-learn sklearn SVM decision boundary / surface
- Plot logarithmic axes
- Plot multiple graphs in one plot using Tensorboard
- Plot yerr/xerr as shaded region rather than error bars
- Plotting a pie chart out of a dictionary

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 courseTrack 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.