Skip to content

add a background color for the arrows in plots by plot_tree #13971

Closed

Description

@stevenlis

Description

There're no arrows drew between nodes and the rotation does not work either.

Steps/Code to Reproduce

from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import plot_tree
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt

X, y = load_iris(return_X_y=True)
tree = DecisionTreeClassifier(criterion='gini')
tree.fit(X, y)
fig, ax = plt.subplots(figsize=(10, 10))
plot_tree(tree, rotate=True, ax=ax);

Actual Results

download

Versions

Could not locate executable g77
Could not locate executable f77
Could not locate executable ifort
Could not locate executable ifl
Could not locate executable f90
Could not locate executable DF
Could not locate executable efl
Could not locate executable gfortran
Could not locate executable f95
Could not locate executable g95
Could not locate executable efort
Could not locate executable efc
Could not locate executable flang
don't know how to compile Fortran code on platform 'nt'

System:
    python: 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)]
executable: C:\Users\liqia\Anaconda3\python.exe
   machine: Windows-10-10.0.17763-SP0

BLAS:
    macros: 
  lib_dirs: 
cblas_libs: cblas

Python deps:
       pip: 19.1
setuptools: 41.0.0
   sklearn: 0.21.1
     numpy: 1.16.2
     scipy: 1.2.1
    Cython: 0.29.7
    pandas: 0.24.2
C:\Users\liqia\Anaconda3\lib\site-packages\numpy\distutils\system_info.py:638: UserWarning: 
    Atlas (http://math-atlas.sourceforge.net/) libraries not found.
    Directories to search for the libraries can be specified in the
    numpy/distutils/site.cfg file (section [atlas]) or by setting
    the ATLAS environment variable.
  self.calc_info()
C:\Users\liqia\Anaconda3\lib\site-packages\numpy\distutils\system_info.py:638: UserWarning: 
    Blas (http://www.netlib.org/blas/) libraries not found.
    Directories to search for the libraries can be specified in the
    numpy/distutils/site.cfg file (section [blas]) or by setting
    the BLAS environment variable.
  self.calc_info()
C:\Users\liqia\Anaconda3\lib\site-packages\numpy\distutils\system_info.py:638: UserWarning: 
    Blas (http://www.netlib.org/blas/) sources not found.
    Directories to search for the sources can be specified in the
    numpy/distutils/site.cfg file (section [blas_src]) or by setting
    the BLAS_SRC environment variable.
  self.calc_info()

Activity

  1. cmarmo commented on Sep 27, 2019

    @cmarmo
    Contributor

    Hi,
    I can't reproduce the arrow problem...
    About the rotation: after checking the code, I think that this option is available only for DOT exportation.
    See the sklearn.tree.export_graphviz() function.

  2. amueller commented on Sep 27, 2019

    @amueller
    Member

    what version of matplotlib are you using?

  3. stevenlis commented on Sep 28, 2019

    @stevenlis
    Author

    @cmarmo @amueller I just tried with Jupyter Lab and it seems like there is something wrong with VS code. If I set facecolor='k' in Jupyter Lab, it will look like this:
    download

    Maybe there should be a little boundary around those arrows?

  4. jolespin commented on Sep 30, 2019

    @jolespin

    I'm getting the same error with mine. Have you found a fix for this?

  5. astrojuanlu commented on Nov 21, 2019

    @astrojuanlu

    Same here, sklearn 0.21.3 and matplotlib 3.1.1 on Jupyter Lab 1.2.3.

  6. cmarmo commented on Nov 21, 2019

    @cmarmo
    Contributor

    Hi everybody, I've just opened a specific issue about the rotate problem (#15694).
    About the arrows: @jolespin, @StevenLi-DS, could you please let us know which version of matplotlib you are using? Just to understand if scikit-learn can do something here or the problem need to be investigated in the dependencies. Thanks!

  7. stevenlis commented on Nov 21, 2019

    @stevenlis
    Author

    @cmarmo I forgot the comment. I think the issue is not the arrows but the color of the arrows doesn't change dynamically based on the facecolor of the plot.

    I can see the arrow now even with a white background, so I think it might be my IDE's issue, i.e. vscode.

    download

    However, the arrows are not visible if you change the facecolor='k' since they don't have a white background below them like the text rectangles.

    download

    matplotlib: 3.1.1

  8. changed the title [-]no rotation and no arrows drew betwee nodes in plot_tree[/-] [+]add a background color for the arrows in plots by plot_tree[/+] on Nov 21, 2019
  9. amueller commented on Jan 6, 2020

    @amueller
    Member

    Instead of facecolor='k' you should probably use plt.style.use('dark_background').
    Though unfortunately we're hard-coding the face color right now, so that doesn't work either. But it seems like we should be able to make that work?

    Silly question: what's wrong with just inverting the image?

  10. amueller commented on Jan 6, 2020

    @amueller
    Member
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.tree import plot_tree
    from sklearn.datasets import load_iris
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    
    plt.style.use('dark_background')
    mpl.rcParams['text.color'] = 'black'
    
    X, y = load_iris(return_X_y=True)
    tree = DecisionTreeClassifier(criterion='gini')
    tree.fit(X, y)
    fig, ax = plt.subplots(figsize=(10, 10), facecolor='k')
    plot_tree(tree, rotate=True, ax=ax);
    plt.show()

    image

  11. stevenlis commented on Jan 6, 2020

    @stevenlis
    Author

    plt.style.use('dark_background') will affect global settings, so maybe put it in a context manager every time?

  12. amueller commented on Jan 7, 2020

    @amueller
    Member

    Indeed, that's a better way to do it ;) Same for the rcParams change.

  13. cmarmo commented on Jan 9, 2020

    @cmarmo
    Contributor

    @amueller, @StevenLi-DS , it seems that everybody is happy now... :)
    Closing?

  14. stevenlis commented on Jan 9, 2020

    @stevenlis
    Author

    @cmarmo It would be better if this can be fixed so that the users don't have to set style every time. But if it requires too much time, I think this workaround is ok for me.

  15. cmarmo commented on Jan 10, 2020

    @cmarmo
    Contributor

    That's why I gave it a try... :)
    But @amueller is the one with merging powers... ;)
    Maybe he could give me some advice about how to improve my PR (#15940)? Thanks!

  16. NicolasHug commented on Jan 17, 2020

    @NicolasHug
    Member

    #13971 (comment) + context manager is a good enough workaround, closing

  17. RushikeshJoshi4 commented on Nov 18, 2020

    @RushikeshJoshi4

    I know this is a closed issue, But if anyone is still wondering, using 'jpg' as file extension instead of 'png' in savefig() solved my problem of arrows being invisible. I'm also using VS by the way.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions