Fuzzy Tree Plotter is a small Python library for turning fuzzy rules into a visual decision tree generated with Graphviz. The project reads text-based rules, organizes them into a tree structure, and produces an image that is ready to inspect or share.
The main class, FuzzyTreePlotter, accepts two kinds of input:
- an in-memory list of rules;
- the path to a
.txt,.csv, or.jsonfile containing rules.
Each rule is parsed, inserted into the internal tree structure, and then exported as a Graphviz file. The final result can be displayed automatically or saved in a format of your choice.
- Python 3.11 or later.
- The Python package
graphviz. - A system-wide Graphviz installation with the
dotcommand available on thePATH.
pip install graphvizGraphviz must be installed separately because the Python package provides integration, but not the rendering engine itself. On Windows, make sure the Graphviz bin folder has been added to the system PATH.
Official download:
https://graphviz.org/download/
Rules must follow this syntax, case-insensitive:
if <attribute> is <value> [and <attribute> is <value> ...] then <outcome>
if F2 is L and F8 is L and F7 is L then 3
if F2 is H and F8 is M and F7 is H then 1
- Every condition must use the form
<attribute> is <value>. - Multiple conditions must be joined with
and. - The part after
thenis the outcome associated with the rule. - Rules can be written in uppercase, lowercase, or mixed case.
from fuzzy_tree_plotter import FuzzyTreePlotter
rules = [
"if F2 is L and F8 is L and F7 is L then 3",
"if F2 is L and F8 is L and F7 is M then 3",
"if F2 is L and F8 is L and F7 is H then 1",
]
plotter = FuzzyTreePlotter(
rules,
aggregate=True,
text_size=20,
line_width=2,
edge_text_size=18,
)
plotter.render("fuzzy_tree", format="png", view=True)The file must contain one rule per line, with no quotes and no extra text.
from fuzzy_tree_plotter import FuzzyTreePlotter
plotter = FuzzyTreePlotter("rules.txt", aggregate=True)
plotter.render("fuzzy_tree", format="png", view=True)The repository includes:
examples/demo_fuzzy_tree_plotter.py, which shows two complete usage patterns;examples/rules.txt, which contains a ready-to-use set of rules for testing;assets/fuzzy_tree_example.png, which shows an example of the final result.
The demo writes its generated artifacts to examples/demo_outputs/ and leaves them on disk for inspection. These files stay local and are not meant to be pushed to the remote repository.
- The class now accepts any string iterable, not just a list or a file path.
- Rules loaded from
.txtfiles ignore blank lines and comments that start with#. .csvfiles are read from the first column, one rule per row, and a simplerule/rulesheader row is ignored..jsonfiles can contain either a list of rules or an object with aruleskey.- The
summary()method returns a compact overview of the tree structure, which is useful for quick inspection. - The
validate_rules()method reports syntax errors, duplicates, and conflicts before rendering. - The
detect_conflicts()method returns only the conflicting rules. - The
render()method can keep or remove the Graphviz source file through thecleanup_sourceparameter. - The
themeparameter controls the overall graph style. - Leaf colors can be customized with the
outcome_colorsmapping. - The demo keeps generated files in
examples/demo_outputs/instead of deleting them. - The main demo now lives under
examples/and uses robustPath-based file handling.
FuzzyTreePlotter(rules, aggregate=False, text_size=12, line_width=1, edge_text_size=12, theme="balanced", outcome_colors=None)
| Parameter | Type | Default | Description |
|---|---|---|---|
rules |
list[str] or str |
required | A list of rules or the path to a .txt, .csv, or .json file |
aggregate |
bool |
False |
Merge leaf nodes that share the same outcome |
text_size |
int |
12 |
Font size for tree nodes |
line_width |
int or float |
1 |
Edge stroke width |
edge_text_size |
int |
12 |
Font size for edge labels |
theme |
str |
"balanced" |
Graph styling profile: balanced, compact, or presentation |
outcome_colors |
dict[str, str] |
None |
Optional mapping of outcomes to custom leaf colors |
Generates the tree graph and saves it to disk.
| Parameter | Type | Default | Description |
|---|---|---|---|
filename |
str |
"fuzzy_tree" |
Output file name without extension |
format |
str |
"png" |
Output format, for example png, pdf, or svg |
view |
bool |
True |
Open the generated file automatically |
cleanup_source |
bool |
True |
Remove the Graphviz source file after rendering |
Returns a dictionary with useful information about the generated tree, including the number of rules, internal nodes, leaf nodes, maximum depth, discovered attributes, and observed outcomes.
Validates all rules and returns a report with syntax errors, duplicate rules, and conflicts where the same conditions lead to different outcomes.
Returns only the conflicting rules discovered during validation.
When aggregate=True, leaf nodes with the same final outcome are grouped together to reduce visual repetition. This is useful when many rules lead to the same result and you want a more compact tree.
This project is distributed under the MIT License. See the LICENSE file for the full text.
Giustino C. Miglionico