Skip to content
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

Adjust Bounding Box of saved svg file #316

Open
lgi1sgm opened this issue Mar 8, 2024 · 0 comments
Open

Adjust Bounding Box of saved svg file #316

lgi1sgm opened this issue Mar 8, 2024 · 0 comments

Comments

@lgi1sgm
Copy link

lgi1sgm commented Mar 8, 2024

Hey there,

I want to use the SVG file coming out of the DTreeViz Render, but the bounding box around it is huge, and when I embed it into html code the tree becomes tiny.

def save(self, filename):

how can I get a tight bounding box around the SVG file?

Example

#!/usr/bin/env python


# Imports
# -------

import os
import dtreeviz

from pathlib import Path
from tempfile import NamedTemporaryFile
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import make_blobs


# Create temp files
# -----------------

tempfile = Path(NamedTemporaryFile().name)
svg_temp = tempfile.with_suffix('.svg')
html_temp = tempfile.with_suffix('.html')

print(f"You can delete this files later:\n{svg_temp}\n{html_temp}")


# Create example decision tree
# ----------------------------

X, y = make_blobs(n_samples=10, n_features=2, centers=3)

d = DecisionTreeClassifier()
d.fit(X, y)


# Export Decision Tree to SVG
# ---------------------------

tree_model = dtreeviz.model(d, X_train=X, y_train=y)
tree_render = tree_model.view()
tree_render.save(str(svg_temp))


# Create a HTML for displaying the SVG
# ------------------------------------

html = f"""
<!doctype html>
<html>
<head>
<style>
body {{margin: auto;max-width: 42em;}}
</style>
</head>
<body>
<img src="{svg_temp}"/>
</body>
"""

with open(html_temp, 'w') as fid:
  fid.write(html)


# Show whats the issue
# --------------------

# Expected Behavior: Graphic should be full size
# Experienced Behavior: A small tree is displayed in the upper left corner of the browser.

os.system('start ' + str(svg_temp))


# Experienced Behavior: Small tree fills only left side.
# Expected Behavior: An image of the tree fills out the complete width.

os.system('start ' + str(html_temp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant