Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I think the setting you are looking for is fontsize . You have to balance it with max_depth and figsize to get a readable plot. Here is an example

from sklearn import tree
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
# load data
X, y = load_iris(return_X_y=True)
# create and train model
clf = tree.DecisionTreeClassifier(max_depth=4)  # set hyperparameter
clf.fit(X, y)
# plot tree
plt.figure(figsize=(12,12))  # set plot size (denoted in inches)
tree.plot_tree(clf, fontsize=10)
plt.show()

If you want to capture structure of the whole tree I guess saving the plot with small font and high dpi is the solution. Then you can open a picture and zoom to the specific nodes to inspect them.

# create and train model
clf = tree.DecisionTreeClassifier()
clf.fit(X, y)
# save plot
plt.figure(figsize=(12,12))
tree.plot_tree(clf, fontsize=6)
plt.savefig('tree_high_dpi', dpi=100)

Here is an example of how it looks like on the bigger tree.

fig, ax = plt.subplots(figsize=(10, 10)) # whatever size you want tree.plot_tree(clf.fit(X, y), ax=ax) plt.show() This does nothing to actually fit the plot_tree to make it legible like the OP wanted. All this does is extend the subplot, and make it fit more items, but does not extend the subplot to an extent where anything is readable as a dynamic way would. – Vaidøtas I. Jan 20, 2020 at 13:19 Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Jul 21, 2022 at 9:05

The problem is solved if you set the size before-hand:

from sklearn.tree import plot_tree, export_text
fig = plt.figure(figsize=(25,20))
_ = plot_tree(clf)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.