Skip to content

tree.plot_tree() does not visualize properly if also using seaborn.set_style()  #16943

Closed

Description

@jlewis-ds

Describe the bug

If using scikit-learn and seaborn together, when using sns.set_style() the plot output from tree.plot_tree() only produces the labels of each split. It does not produce the nodes or arrows to actually visualize the tree.

Steps/Code to Reproduce

import seaborn as sns
sns.set_style('whitegrid') #Note: this can be any option for set_style
from sklearn.datasets import load_iris
from sklearn import tree

clf = tree.DecisionTreeClassifier(random_state=0)
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
plt.show(tree.plot_tree(clf))

Expected Results

Tree is properly visualized.

Actual Results

Only labels are produced, not the tree itself.

Versions

System:
python: 3.7.7 (default, Mar 23 2020, 23:19:08) [MSC v.1916 64 bit (AMD64)]
executable: C:\Users\jlewis\AppData\Local\Continuum\anaconda3\python.exe
machine: Windows-10-10.0.17763-SP0

Python dependencies:
pip: 20.0.2
setuptools: 46.1.3.post20200330
sklearn: 0.22.1
numpy: 1.18.1
scipy: 1.4.1
Cython: None
pandas: 1.0.3
matplotlib: 3.1.3
joblib: 0.14.1

Built with OpenMP: True
tree_plot

tree_plot_with_setstyle

Activity

  1. cmarmo commented on Apr 20, 2020

    @cmarmo
    Contributor

    Hi @jlewis-ds this is probably related to #13971 because of the whitegrid seaborn definition.

                style_dict.update({
    
                    "axes.facecolor": "white",
                    "axes.edgecolor": light_gray,
                    "grid.color": light_gray,
    
                    "axes.spines.left": True,
                    "axes.spines.bottom": True,
                    "axes.spines.right": True,
                    "axes.spines.top": True,
    
                    })

    @amueller is the most suitable to give you an answer.

  2. amueller commented on May 11, 2020

    @amueller
    Member

    In a sense it's the opposite problem. In #13971 the user tried to change the color, which you can do via the RC. Here, @jlewis-ds changed the rc colors but didn't want it to affect the output.

    We can either hard-code the colors and people can't change the colors, or we can use the colors that user specified.
    Here, it's axes.edgecolor I think that is set by nearly all styles in seaborn. So it looks like the seaborn styles intentionally hide all arrows or borders or really anything that's not given explicit color.

    We could use a different color, say the text color, to draw the arrows. But that's basically hacking around the opinion of seaborn. Seaborn asked for all borders to be white, and all borders turn white.

  3. amueller commented on May 11, 2020

    @amueller
    Member

    I would say this is the pragmatic solution: #17187
    it keeps some of the styling while also making sure the arrows are drawn.

  4. jlewis-ds commented on May 11, 2020

    @jlewis-ds
    Author

    #17187 looks like a good work around. I was hesitant to report this as a bug since technically everything is doing what it's supposed to, but it seemed like an unintentional result which could easily be overlooked.

    Thanks, I'm happy to call this closed.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions