8000 plot_tree diagram is cropped in iris example · Issue #21908 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

plot_tree diagram is cropped in iris example #21908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lesteve opened this issue Dec 7, 2021 · 9 comments · Fixed by #21917
Closed

plot_tree diagram is cropped in iris example #21908

lesteve opened this issue Dec 7, 2021 · 9 comments · Fixed by #21917

Comments

@lesteve
Copy link
Member
lesteve commented Dec 7, 2021

https://scikit-learn.org/stable/auto_examples/tree/plot_iris_dtc.html

image

Note: I tried fixing it quickly by playing with the figsize to no avail. Looks like you can see the full diagram on the matplotlib figure but the diagram is cropped when saving to a png.

Looks like it was working fine in 0.24 (maybe a regression or maybe some changes in matplotlib hard to tell ...):
https://scikit-learn.org/0.24/auto_examples/tree/plot_iris_dtc.html

@glemaitre
Copy link
Member

Is a plt.tight_layout would do the job?

@lesteve
Copy link
Member Author
lesteve commented Dec 7, 2021

Nope, the png is still cropped.

@lesteve
Copy link
Member Author
lesteve commented Dec 7, 2021

Quick and dirty reproducer:

# %%
import numpy as np
import matplotlib.pyplot as plt

from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, plot_tree

# Load data
iris = load_iris()

plt.figure()
clf = DecisionTreeClassifier().fit(iris.data, iris.target)
plot_tree(clf, filled=True)
plt.savefig('/tmp/tree.png')
# %%
from matplotlib.image import imread

plt.figure()
plt.imshow(imread('/tmp/tree.png'))

The matplotlib figure looks fine:
image

But rereading the png shows the diagram is cropped:
image

@lesteve
Copy link
Member Author
lesteve commented Dec 7, 2021

Seems like a regression in 1.0, the png is fine with scikit-learn 0.24.2 and keeping the same matplotlib version (3.4.2 for completeness).

@glemaitre
Copy link
Member

I have two candidates PRs for introducing the regression:

Otherwise we did not change anything that should impact the tree plotting

@lesteve
Copy link
Member Author
lesteve commented Dec 7, 2021

Seems to be culprit #20023. I looked a bit at the code but this looks a bit too much for my matplotlib skills, maybe @thomasjpfan has some insights into this?

@thomasjpfan
Copy link
Member

#20023 is the issue. Reverting it would also lead to another regression. I'm looking into a fix.

@das-ankur
Copy link

Can resizing the image when it's opened by imread() solve the issue?

@thomasjpfan
Copy link
Member

Can resizing the image when it's opened by imread() solve the issue?

That would not resolve the issue because the saved image itself is drawn incorrectly. One can test this out by running:

import matplotlib.pyplot as plt

from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, plot_tree

# Load data
iris = load_iris()

plt.figure()
clf = DecisionTreeClassifier().fit(iris.data, iris.target)
plot_tree(clf, filled=True)
plt.savefig('tree.png')

and open tree.png file in an image viewer.

I opened #21917 to resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0