相关文章推荐
腼腆的柠檬  ·  python ...·  1 周前    · 
有情有义的大白菜  ·  python ...·  1 周前    · 
完美的馒头  ·  python QTreeWidget ...·  1 周前    · 
失眠的烤红薯  ·  python qt textBrowser ...·  1 周前    · 
非常酷的匕首  ·  docker逃逸总结 - ...·  9 月前    · 
力能扛鼎的夕阳  ·  在 PHP ...·  1 年前    · 
强健的手套  ·  javascript - Using ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

When I plot my sklearn decision tree using sklearn.tree.plot_tree() , the nodes are overlapping on the deeper levels and I cannot read what is in the nodes. It is not nice to present your results. It looks like this:

How can I achieve having more space between them, to avoid an overlap?

I already found this question, and I already increased the fontsize to 10 and figsize to (20,30), but it leads to the tree plotted as shown in the picture. I also increased dpi to 150. This is my code:

fig = plt.figure(figsize=(20,30))
artists = sklearn.tree.plot_tree(decision_tree=clf, feature_names=feature_names, class_names=class_names, filled=True, rounded=True, fontsize=10, max_depth=4)
fig.savefig(filepath, dpi=150)

Just increase figsize=(50,30), adjust dpi=300 and apply the code to save the image in png. This saved image should look better

fig = plt.figure(figsize=(50,30))
artists = sklearn.tree.plot_tree(decision_tree=clf, feature_names=feature_names, class_names=class_names, filled=True, rounded=True, fontsize=10, max_depth=4,dpi=300) #adjust the dpi to the parameter that fits best your output
plt.savefig('decision_tree.png')#save the tree image in png format
plt.show()
                plot_tree does not include "dpi". That is either used when creating the figure or in plt.savefig()
– Artur
                Oct 10, 2022 at 7:30

You need to control the size of rendering the image. Try changing the figsize like fig = plt.figure(figsize=(50,30)). This worked for me.
You can find the original documentation in https://scikit-learn.org/stable/modules/generated/sklearn.tree.plot_tree.html.

I appreciate your effort to answer me, but as described in the question, I already tried this and it did not work. – LBoss May 21, 2021 at 8:17

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.