chico0913
chico0913

Reputation: 647

How can I adjust the size of the plot_tree graph in sklearn to make it readable?

I am trying to plot a plot_tree object from sklearn with matplotlib, but my tree plot doesn't look good. My tree plot looks squished:

enter image description here

Below are my code:

from sklearn import tree
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

# create tree object 
model_gini_class = tree.DecisionTreeClassifier(criterion='gini')

# train the model using the training sets and check score
model_gini_class.fit(X_train, y_train)
model_gini_class.score(X_train, y_train)

# predict output
predicted_gini_class = model_gini_class.predict(X_test)

plt.figure()
tree.plot_tree(model_gini_class, filled=True)
plt.title("Decision trees on the Shakespear dataset (Gini)")
plt.show() # the tree looks squished?

So my questions is:

Thank you,

Upvotes: 8

Views: 23534

Answers (3)

Antoine Krajnc
Antoine Krajnc

Reputation: 1323

You can do two things:

Method 1


# Decision tree
classifier = DecisionTreeClassifier()
classifier.fit(X_train, y_train)


_, ax = plt.subplots(figsize=(30,30)) # Resize figure
plot_tree(classifier, filled=True, ax=ax)
plt.show()

Method 2


# Decision tree
classifier = DecisionTreeClassifier()
classifier.fit(X_train, y_train)

plt.figure(figsize=(30, 30) # Resize figure
plot_tree(classifier, filled=True)
plt.show()

Whatever you prefer using

Upvotes: 4

Shayak Roy Chowdhury
Shayak Roy Chowdhury

Reputation: 19

this might help

from matplotlib import pyplot as plt
fig, axes = plt.subplots(nrows = 1,ncols = 1,figsize = (5,5), dpi=300)
tree.plot_tree(model_gini_class, filled=True)

Upvotes: 1

Ozgur
Ozgur

Reputation: 172

this might help

plt.figure(figsize=(10,10))

Upvotes: 6

Related Questions