Sklearn plot_tree plot is too small Sklearn plot_tree plot is too small python python

Sklearn plot_tree plot is too small


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 treefrom sklearn.datasets import load_irisimport matplotlib.pyplot as plt# load dataX, y = load_iris(return_X_y=True)# create and train modelclf = tree.DecisionTreeClassifier(max_depth=4)  # set hyperparameterclf.fit(X, y)# plot treeplt.figure(figsize=(12,12))  # set plot size (denoted in inches)tree.plot_tree(clf, fontsize=10)plt.show()

enter image description here

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 modelclf = tree.DecisionTreeClassifier()clf.fit(X, y)# save plotplt.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.

enter image description here

enter image description here


What about setting the size of the image before hand:

clf = tree.DecisionTreeClassifier()clf = clf.fit(X, y)fig, ax = plt.subplots(figsize=(10, 10))  # whatever size you wanttree.plot_tree(clf.fit(X, y), ax=ax)plt.show()