Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To visualize a tree from a fitted scikit-learn random forest, select one estimator from the forest’s estimators_ collection and pass it to sklearn.tree.plot_tree. Supply feature names in the same column order used to fit the forest; for classification, also match class labels to the fitted estimator’s class order. The result shows that individual tree—not the forest’s complete prediction process.

Plot one tree with scikit-learn’s Matplotlib function

This example assumes forest is an already-fitted RandomForestClassifier or RandomForestRegressor. Set feature_names to the names of the columns, in exactly the order presented to the forest during fitting. For classification, set class_names to labels aligned with the selected tree’s classes_; omit that argument for regression.

import matplotlib.pyplot as plt
from sklearn.tree import plot_tree

# forest is an already-fitted RandomForestClassifier or RandomForestRegressor.
# feature_names must match the exact input-column order used when fitting.
tree = forest.estimators_[0]

plt.figure(figsize=(20, 10))
plot_tree(
    tree,
    feature_names=feature_names,
    class_names=class_names,  # classification only; omit for regression
    filled=True,
    rounded=True,
    max_depth=3,
    proportion=True,
    fontsize=9,
)
plt.tight_layout()
plt.show()

The example limits the displayed tree to depth 3 for readability. That setting hides deeper splits, so describe the resulting diagram as truncated rather than as the complete tree. Figure size, font size, and the depth limit can be adjusted for the data and output. See the scikit-learn plot_tree API reference for the parameters available in the documentation version matching your installed scikit-learn release.

How to show the right feature and class names

Match feature names to the model input

plot_tree labels features positionally. If you omit feature_names, the plot uses generic positional labels rather than your descriptive column names. The names must correspond to the exact feature matrix passed to the forest, in the same order.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If preprocessing changes the input—for example, one-hot encoding or selecting columns—use the transformed feature names in the order seen by the forest, not the original raw-column names. Otherwise, a visually polished plot can describe the wrong feature or mislabel a split.

Align classification labels with the estimator

For a classifier, inspect the selected tree’s classes_ and ensure the labels supplied through class_names follow that order. A mismatched label order can make the plot’s class labels misleading. Regression trees do not use class names.

Rank #2
Sale
Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems
  • Use scikit-learn to track an example ML project end to end
  • Explore several models, including support vector machines, decision trees, random forests, and ensemble methods
  • Exploit unsupervised learning techniques such as dimensionality reduction, clustering, and anomaly detection
  • Dive into neural net architectures, including convolutional nets, recurrent nets, generative adversarial networks, autoencoders, diffusion models, and transformers
  • Use TensorFlow and Keras to build and train neural nets for computer vision, natural language processing, generative models, and deep reinforcement learning

Choose a plotting or export method

Method Output and use What it requires
plot_tree A graphical tree rendered with Matplotlib; convenient for notebooks and inline figures. Matplotlib. The API includes controls such as max_depth, feature_names, class_names, filled, impurity, node_ids, proportion, rounded, precision, and fontsize. See the API reference.
export_graphviz Graphviz DOT text for a graphical tree artifact. A Graphviz renderer, such as the dot command, to turn DOT output into a graphic. The function returns DOT text; it does not render the image by itself. See the API reference.
export_text A compact textual rules report for inspection or text-based output. No graphical renderer; this option is text, not a graphical visualization. See the API reference.

Use plot_tree for a quick inline diagram, export_graphviz when you need a separate rendered graphic and can use Graphviz, or export_text when a dense picture is impractical and readable rules are more useful.

What a plotted tree says about the random forest

A random forest is an ensemble of trees. Scikit-learn describes its forest construction as using sample bootstrapping and randomized feature selection, then combining the individual tree predictions. A plot of forest.estimators_[0] reveals the splits and output of that one member; it is not a display of the ensemble’s full decision process. The scikit-learn ensemble guide explains the forest construction and the role of its randomness.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If you are explaining a particular case, compare the selected tree’s output with the forest’s prediction. Also state which member you plotted: another estimator, training sample, or random state can produce a different tree. Unless you have a reasoned selection method, do not imply the first estimator is uniquely representative of the forest.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Fix common plotting problems

  • The call fails or does not plot the forest: pass a fitted decision-tree estimator, not the forest object itself. Select a member such as forest.estimators_[0].
  • Feature labels are generic or incorrect: provide names matching the fitted input matrix’s exact column order, including any transformed feature names.
  • Class labels do not match the plotted outcomes: align class_names with the selected estimator’s classes_.
  • The diagram is too crowded: set a smaller max_depth, adjust the Matplotlib figure dimensions or font size, or use export_text for a compact rules view. Disclose when a depth limit makes the plot partial.
  • Graphviz output is only text: render the DOT returned by export_graphviz with Graphviz, such as its dot command.

Scikit-learn’s API and defaults can change between releases. Check the documentation corresponding to your installed version for the precise parameter availability and behavior.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.